JqueryValidate相关参数及常⽤的⾃定义验证规则⼀、官⽹地址:
⼆、默认校验规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17(1) required:true必输字段
(2) remote:"remote-valid.jsp"使⽤ajax⽅法调⽤remote-valid.jsp验证输⼊值
(3) email:true必须输⼊正确格式的电⼦邮件
(4) url:true必须输⼊正确格式的⽹址
(5) date:true必须输⼊正确格式的⽇期,⽇期校验ie6出错,慎⽤
(6) dateISO:true必须输⼊正确格式的⽇期(ISO),例如:2009-06-23,1998/01/22 只验证格式,不验证有效性
(7) number:true必须输⼊合法的数字(负数,⼩数)
(8) digits:true必须输⼊整数
(9) creditcard:true必须输⼊合法的信⽤卡号
(10) equalTo:"#password"输⼊值必须和#password相同
(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 输⼊值不能⼩于10
三、默认的提⽰1
2
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21messages: {
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
dateDE: "Bitte geben Sie ein g眉ltiges Datum ein.",
number: "Please enter a valid number.",
numberDE: "Bitte geben Sie eine Nummer ein.",
jquery怎么进行验证digits: "Please enter only digits",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: $.validator.format("Please enter no more than {0} characters."),
minlength: $.validator.format("Please enter at least {0} characters."),
rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."), range: $.validator.format("Please enter a value between {0} and {1}."),
max: $.validator.format("Please enter a value less than or equal to {0}."),
min: $.validator.format("Please enter a value greater than or equal to {0}.")
},
⽰例:
$("#fm").validate({
rules:{
loginpwd:{
required:true,
minlength:6,
maxlength:12,
regex:"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$"
},
loginpwd2:{
required:true,
equalTo: "#loginpwd",
regex:"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$"
}
},
messages:{
loginpwd:{
required: "请输⼊密码",
minlength: "密码长度不能⼩于 6 个字母",
regex:"密码必须包含⼤⼩写字母和数字的组合,不能使⽤特殊字符,长度在8-10之间" },
loginpwd2:{
required: "请输⼊密码",
minlength: "密码长度不能⼩于 6 个字母",
equalTo: "两次密码输⼊不⼀致",
regex:"密码必须包含⼤⼩写字母和数字的组合,不能使⽤特殊字符,长度在8-10之间" }
}
});
四、Jquery Validate ⾃定义验证规则
addMethod(name,method,message)⽅法
参数 name 是添加的⽅法的名字。
参数 method 是⼀个函数,接收三个参数 (value,element,param) 。
value 是元素的值,element 是元素本⾝,param 是参数。
⾝份证号码验证
jQuery.validator.addMethod(“idcardno”, function(value, element) {
return this.optional(element) || isIdCardNo(value);
}, “请正确输⼊⾝份证号码”);
字母数字
jQuery.validator.addMethod(“alnum”, function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
}, “只能包括英⽂字母和数字”);
验证
jQuery.validator.addMethod(“zipcode”, function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (st(value));
}, “请正确填写”);
汉字
jQuery.validator.addMethod(“chcharacter”, function(value, element) {
var tel = /^[u4e00-u9fa5]+$/;
return this.optional(element) || (st(value));
}, “请输⼊汉字”);
字符最⼩长度验证(⼀个中⽂字符长度为2)
jQuery.validator.addMethod(“stringMinLength”, function(value, element, param) {
var length = value.length;
for(var i = 0; i < value.length; i++) {
if(value.charCodeAt(i) > 127) {
length++;
}
}
return this.optional(element) || (length >= param);
}, $.validator.format(“长度不能⼩于 { 0 }!”));
字符最⼤长度验证(⼀个中⽂字符长度为2)
jQuery.validator.addMethod(“stringMaxLength”, function(value, element, param) {
var length = value.length;
for(var i = 0; i < value.length; i++) {
if(value.charCodeAt(i) > 127) {
length++;
}
}
return this.optional(element) || (length <= param);
}, $.validator.format(“长度不能⼤于 { 0 }!”));
字符验证
jQuery.validator.addMethod(“string”, function(value, element) {
return this.optional(element) || /^[u0391-uFFE5w]+$/.test(value);
}, “不允许包含特殊符号!”);
⼿机号码验证
jQuery.validator.addMethod(“mobile”, 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)); }, “⼿机号码格式错误!”);
电话号码验证
jQuery.validator.addMethod(“phone”, function(value, element) {
var tel = /^(d{3,4}-?)?d{7,9}$/g;
return this.optional(element) || (st(value));
}, “电话号码格式错误!”);
必须以特定字符串开头验证
jQuery.validator.addMethod(“begin”, function(value, element, param) {
var begin = new RegExp(“ ^ ”+param);
return this.optional(element) || (st(value));
}, $.validator.format(“必须以 { 0 } 开头!”));
验证两次输⼊值是否不相同
jQuery.validator.addMethod(“notEqualTo”, function(value, element, param) {
return value != $(param).val();
}, $.validator.format(“两次输⼊不能相同!”));
验证值不允许与特定值等于
jQuery.validator.addMethod(“notEqual”, function(value, element, param) {
return value != param;
}, $.validator.format(“输⼊值不允许为 { 0 }!”));
验证值必须⼤于特定值(不能等于)
jQuery.validator.addMethod(“gt”, function(value, element, param) {
return value > param;
}, $.validator.format(“输⼊值必须⼤于 { 0 }!”));
⼩数点前最多9位,⼩数点后最多6位
jQuery.validator.addMethod("decimal", function (value, element) {
return this.optional(element) || /^([1-9]\d{0,8}|0)(\.\d{1,6})?$/.test(value);
}, "⼩数点前最多9位,⼩数点后最多6位^_^");
结束时间不能⼩于开始时间
jQuery.validator.addMethod("laterTo", function (value, element, param) {
return $(param).val().split("-").join("") < $(element).val().split("-").join("");
}, "结束时间不能⼩于开始时间^_^");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论