jquery.validate.js是jquery下的一个验证插件,功能比较强大,早就有所耳闻但是一只没有动手用过,现在在于能够研究一下了。

这里转载一篇前辈写的文章,在我自己的理解上修改了一下,仅作记录。

先贴一个国内某大公司的代码:

 

JavaScript代码
  1. <script type="text/javascript">   
  2.   
  3. function lang(key) {   
  4.     mylang {   
  5.   
  6.         'ls_input_myb''请输入您的账户',   
  7.         'ls_myb_email''漫游币账户为邮箱地址',   
  8.         'ls_login_password''请输入您的登录密码',   
  9.         'ls_password_length''密码长度为{0}-{1}位之间',   
  10.         'ls_input_captcha''请输入验证码',   
  11.         'ls_captcha_length''验证码的长度为{0}位',   
  12.         'ls_account_email''账户名为邮箱地址',   
  13.   
  14.         '':''  
  15.     };   
  16.   
  17.     return mylang[key];   
  18. }   
  19.   
  20. </script>   
  21.   
  22. <script type="text/javascript">   
  23. $(document).ready(function() {   
  24.   
  25.     $("#loginForm").validate({   
  26.         rules: {   
  27.             uEmail: {   
  28.                 required: true,   
  29.                 email: true  
  30.             },   
  31.             uPassword: {   
  32.                 required: true,   
  33.                 rangelength: [6, 30]   
  34.             }   
  35.         },   
  36.         messages: {   
  37.             uEmail: {   
  38.                 required: lang('ls_input_myb'),   
  39.                 email: lang('ls_myb_email')   
  40.             },   
  41.             uPassword: {   
  42.                 required: lang('ls_login_password'),   
  43.                 rangelength: $.format(lang('ls_password_length'))   
  44.             }   
  45.         },   
  46.         errorPlacement: function(error, element) {   
  47.             var placement $(element.parent("td").parent("tr").next("tr").find("td").get(1));   
  48.             placement.text('');   
  49.             error.appendTo( placement );   
  50.         },   
  51.         onkeyup: false  
  52.     });   
  53.   
  54.     var accountTipsText lang('ls_account_email');   
  55.     $("#uEmail").focus(function() {   
  56.         if (!$($(this).parent().parent().next().find('td').get(1)).text()) {   
  57.             $($(this).parent().parent().next().find('td').get(1)).html('<span class="font_888_8">' accountTipsText '</span>');   
  58.         }   
  59.         $(this).css('color''#000');   
  60.     }).focus();   
  61.   
  62.   
  63.   
  64. });   
  65.   
  66. </script>   

我就是从这个例子中开始的,其实这个例子几乎包含了jquery.validate.js的精髓,如果你完整理解了这个代码基本上算是入门了。

想起以前做期货网页在线模拟的时候都自己写代码去判断,真实幼稚死了…………

下面是完整的文章介绍。

默认校验规则
(1)required:true               必输字段
(2)remote:"check.php"          使用ajax方法调用check.php验证输入值
(3)email:true                  必须输入正确格式的电子邮件
(4)url:true                    必须输入正确格式的网址
(5)date:true                   必须输入正确格式的日期
(6)dateISO:true                必须输入正确格式的日期(ISO),例如:2009-06-23,1998/01/22 只验证格式,不验证有效性
(7)number:true                 必须输入合法的数字(负数,小数)
(8)digits:true                 必须输入整数
(9)creditcard:                 必须输入合法的信用卡号
(10)equalTo:"#field"           输入值必须和#field相同
(11)accept:                    输入拥有合法后缀名的字符串(上传文件的后缀)
(12)maxlength:5                输入长度最多是5的字符串(汉字算一个字符)
(13)minlength:10               输入长度最小是10的字符串(汉字算一个字符)
(14)rangelength:[5,10]         输入长度必须介于 5 和 10 之间的字符串")(汉字算一个字符)
(15)range:[5,10]               输入值必须介于 5 和 10 之间
(16)max:5                      输入值不能大于5
(17)min:10    

相关文章:

  • 2021-12-24
  • 2021-11-04
  • 2021-07-08
  • 2021-08-25
  • 2021-04-12
  • 2021-12-22
猜你喜欢
  • 2021-07-03
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2021-06-04
  • 2021-10-03
  • 2021-07-23
相关资源
相似解决方案