使用JSON规则验证HTML表单

时间:2013-01-30 14:57:30

标签: javascript jquery html json validation

我想使用JSON创建表单,还需要验证JSON中定义的规则。

以下是我的JSON的外观:

{
   "modules":[
      {
         "type":"form",
         "action":"submit.aspx",
         "name":"registrationform",
         "container":"#maincontent",
         "fields":[
            {
               "id":"firstname",
               "label":"First Name",
               "name":"fname",
               "type":"text",
               "value":""
            },
            {
               "id":"email",
               "label":"Email",
               "name":"email",
               "type":"text",
               "value":""
            }
         ],
         "rules":
            {
                "fname" : "required", 
                "email"     :   {
                                    "required": "true",
                                    "email": "true"
                                }
            },
         "messages":
            {
                "fname" : "Enter your firstname",
                "email" :   {
                                "required": "Please enter a valid email address",
                                "minlength": "Please enter a valid email address"
                            }
            }
      }
   ]
}

如何编写动态代码,使用JSON规则验证我的表单元素并显示错误消息(如JSON中所定义)?

是否有一些我可以直接使用的jQuery方法?

0 个答案:

没有答案