jQuery Validation范例

<script src=”/skin/js/jquery.validate.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$().ready(function() {

// 手机号码验证
jQuery.validator.addMethod(”isMobile”, function(value, element) {
var length = value.length;
return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/.test(value));
}, “请正确填写您的手机号码,格式13888888888″);

// 电话号码验证
jQuery.validator.addMethod(”isPhone”, function(value, element) {
var tel = /^[0-9-]{10,13}$/;
return this.optional(element) || (tel.test(value));
}, “请正确填写您的电话/传真号码,格式010-88888888″);

// 邮政编码验证
jQuery.validator.addMethod(”isZipCode”, function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, “请正确填写您的邮政编码,格式100000″);

$(”#aspnetForm”).validate({
rules: {
ctl00$CommonLeft$txtName: {
required: true,
minlength: 2
},
ctl00$CommonLeft$txtZip: {
isZipCode: true
},
ctl00$CommonLeft$txtPhone: {
isPhone: true
},
ctl00$CommonLeft$txtFax: {
isPhone: true
},
ctl00$CommonLeft$txtMobile: {
isMobile: true
},
ctl00$CommonLeft$txtEmail: {
email: true,
minlength:5
},
ctl00$CommonLeft$txtWebSite: {
url:true,
minlength:5
},
ctl00$CommonLeft$txtValidateKey: “required”
},
messages: {
ctl00$CommonLeft$txtName: {
required: “请输入您的真实姓名”,
minlength: “姓名至少2位以上”
},
ctl00$CommonLeft$txtEmail: {
email: “请输入合法的电子邮件地址”,
minlength: “邮件地址至少5位以上字符或数字”
},
ctl00$CommonLeft$txtWebSite: {
url: “请输入合法的网站地址”,
minlength: “邮件地址至少5位以上字符或数字”
},
ctl00$CommonLeft$txtValidateKey: “请输入验证码”
}
});
});
</script>

 

你可能感兴趣的:(JavaScript,jquery)