<pre>
jquery.validate.js使用之自定義表單驗證規則,下面列出了一些經常使用的驗證法規則
jquery.validate.js演示查看 <a href="http://www.51xuediannao.com/js/jquery/jquery_validate/" target="_blank">jquery validate強大的jquery表單驗證插件</a> http://www.51xuediannao.com/js/jquery/jquery_validate/jquery
如:input type="email" value="" name="title" class="fn-tinput" required data-rule-email="true" data-msg-required="請輸入email地址" data-msg-email="請輸入正確的email地址"ui
required //必填this
data-rule-email="true" //正則爲 email編碼
data-msg-required="請輸入您的電子郵箱" //爲空時的提示信息spa
data-msg-email="郵箱格式不正確" //不符合正則的提示插件
placeholder="電子郵件" //input中沒有填寫的提示ip
========================================================
//擴展驗證規則
//郵箱 表單驗證規則
jQuery.validator.addMethod("mail", function (value, element) {
var mail = /^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$/;
return this.optional(element) || (mail.test(value));
}, "郵箱格式不對");
//電話驗證規則
jQuery.validator.addMethod("phone", function (value, element) {
var phone = /^0\d{2,3}-\d{7,8}$/;
return this.optional(element) || (phone.test(value));
}, "電話格式如:0371-68787027");
//區號驗證規則
jQuery.validator.addMethod("ac", function (value, element) {
var ac = /^0\d{2,3}$/;
return this.optional(element) || (ac.test(value));
}, "區號如:010或0371");
//無區號電話驗證規則
jQuery.validator.addMethod("noactel", function (value, element) {
var noactel = /^\d{7,8}$/;
return this.optional(element) || (noactel.test(value));
}, "電話格式如:68787027");
//手機驗證規則
jQuery.validator.addMethod("mobile", function (value, element) {
var mobile = /^1[3|4|5|7|8]\d{9}$/;
return this.optional(element) || (mobile.test(value));
}, "手機格式不對");
//郵箱或手機驗證規則
jQuery.validator.addMethod("mm", function (value, element) {
var mm = /^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/;
return this.optional(element) || (mm.test(value));
}, "格式不對");
//電話或手機驗證規則
jQuery.validator.addMethod("tm", function (value, element) {
var tm=/(^1[3|4|5|7|8]\d{9}$)|(^\d{3,4}-\d{7,8}$)|(^\d{7,8}$)|(^\d{3,4}-\d{7,8}-\d{1,4}$)|(^\d{7,8}-\d{1,4}$)/;
return this.optional(element) || (tm.test(value));
}, "格式不對");
//年齡 表單驗證規則
jQuery.validator.addMethod("age", function(value, element) {
var age = /^(?:[1-9][0-9]?|1[01][0-9]|120)$/;
return this.optional(element) || (age.test(value));
}, "不能超過120歲");
///// 20-60 /^([2-5]\d)|60$/
//傳真
jQuery.validator.addMethod("fax",function(value,element){
var fax = /^(\d{3,4})?[-]?\d{7,8}$/;
return this.optional(element) || (fax.test(value));
},"傳真格式如:0371-68787027");
//驗證當前值和目標val的值相等 相等返回爲 false
jQuery.validator.addMethod("equalTo2",function(value, element){
var returnVal = true;
var id = $(element).attr("data-rule-equalto2");
var targetVal = $(id).val();
if(value === targetVal){
returnVal = false;
}
return returnVal;
},"不能和原始密碼相同");
//大於指定數
jQuery.validator.addMethod("gt",function(value, element){
var returnVal = false;
var gt = $(element).data("gt");
if(value > gt && value != ""){
returnVal = true;
}
return returnVal;
},"不能小於0 或空");
//漢字
jQuery.validator.addMethod("chinese", function (value, element) {
var chinese = /^[\u4E00-\u9FFF]+$/;
return this.optional(element) || (chinese.test(value));
}, "格式不對");
//指定數字的整數倍
jQuery.validator.addMethod("times", function (value, element) {
var returnVal = true;
var base=$(element).attr('data-rule-times');
if(value%base!=0){
returnVal=false;
}
return returnVal;
}, "必須是發佈賞金的整數倍");
//身份證
jQuery.validator.addMethod("idCard", function (value, element) {
var isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;//(15位)
var isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;//(18位)
return this.optional(element) || (isIDCard1.test(value)) || (isIDCard2.test(value));
}, "格式不對");
// 字符驗證
jQuery.validator.addMethod("stringCheck", function(value, element) {
return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
}, "只能包括中文字、英文字母、數字和下劃線");
------------------------------------------------------------
// 中文字兩個字節
jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
var length = value.length;
for(var i = 0; i <</span> value.length; i++){
if(value.charCodeAt(i) > 127){
length++;
}
}
return this.optional(element) || ( length >= param[0] && length <= param[1] );
}, "請確保輸入的值在3-15個字節之間(一箇中文字算2個字節)");
------------------------------------------------------------
// 身份證號碼驗證
jQuery.validator.addMethod("isIdCardNo", function(value, element) {
return this.optional(element) || isIdCardNo(value);
}, "請正確輸入您的身份證號碼");
------------------------------------------------------------
// 手機號碼驗證
jQuery.validator.addMethod("isMobile", function(value, element) {
var length = value.length;
var mobile =/^[1][3-8]+\\d{9}/;
return this.optional(element) || (length == 11 && mobile.test(value));
}, "請正確填寫您的手機號碼");
------------------------------------------------------------
// 電話號碼驗證
jQuery.validator.addMethod("isTel", function(value, element) {
var tel = /^\d{3,4}-?\d{7,9}$/; //電話號碼格式010-12345678
return this.optional(element) || (tel.test(value));
}, "請正確填寫您的電話號碼");
------------------------------------------------------------
// 聯繫電話(手機/電話皆可)驗證
jQuery.validator.addMethod("isPhone", function(value,element) {
var length = value.length;
var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
var tel = /^\d{3,4}-?\d{7,9}$/;
return this.optional(element) || (tel.test(value) || mobile.test(value));
}, "請正確填寫您的聯繫電話");
------------------------------------------------------------
// 郵政編碼驗證
jQuery.validator.addMethod("isZipCode", function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "請正確填寫您的郵政編碼");
jquery.validate.js表單驗證規則,<a href="http://www.51xuediannao.com/" target="_blank">懶人建站</a>整理,不斷更新中...
</pre>element