jquery.validate.js的基本用法入门[转]
jquery.validate.js是jquery下的一个验证插件,功能比较强大,早就有所耳闻但是一只没有动手用过,现在在于能够研究一下了。
这里转载一篇前辈写的文章,在我自己的理解上修改了一下,仅作记录。
先贴一个国内某大公司的代码:
JavaScript代码
- <script
type="text/javascript"> - function
lang(key) { mylang = { 'ls_input_myb': '请输入您的账户', 'ls_myb_email': '漫游币账户为邮箱地址', 'ls_login_password': '请输入您的登录密码', 'ls_password_length': '密码长度为{0}-{1}位之间', 'ls_input_captcha': '请输入验证码', 'ls_captcha_length': '验证码的长度为{0}位', 'ls_account_email': '账户名为邮箱地址', '':'' }; return mylang[key]; - }
- </script>
- <script
type="text/javascript"> - $(document).ready(function()
{ $("#loginForm").validate({ rules: { uEmail: { required: true, email: true }, uPassword: { required: true, rangelength: [6, 30] } }, messages: { uEmail: { required: lang('ls_input_myb'), email: lang('ls_myb_email') }, uPassword: { required: lang('ls_login_password'), rangelength: $.format(lang('ls_password_length')) } }, errorPlacement: function(error, element) { var placement = $(element.parent("td").parent("tr").next("tr").find("td").get(1)); placement.text(''); error.appendTo( placement ); }, onkeyup: false }); var accountTipsText = lang('ls_account_email'); $("#uEmail").focus(function() { if (!$($(this).parent().parent().next().find('td').get(1)).text()) { $($(this).parent().parent().next().find('td').get(1)).html('<span class="font_888_8">' + accountTipsText + '</span>'); } $(this).css('color', '#000'); }).focus(); - });
- </script>
我就是从这个例子中开始的,其实这个例子几乎包含了jquery.validate.js的精髓,如果你完整理解了这个代码基本上算是入门了。
想起以前做期货网页在线模拟的时候都自己写代码去判断,真实幼稚死了…………
下面是完整的文章介绍。
默认校验规则
(1)required:true
(2)remote:"check.php"
(3)email:true
(4)url:true
(5)date:true
(6)dateISO:true
(7)number:true
(8)digits:true
(9)creditcard:
(10)equalTo:"#field"
(11)accept:
(12)maxlength:5
(13)minlength:10
(14)rangelength:[5,10]
(15)range:[5,10]
(16)max:5
(17)min:10
- 上一篇 »Python爬虫入门:爬虫基础了解
- 下一篇 »python入门:while 循环的基本用法