这里转载一篇前辈写的文章,在我自己的理解上修改了一下,仅作记录。
先贴一个国内某大公司的代码:
<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” 使用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: 必须输入合法的信用卡号










