jquery.validate.js使用之自定义表单验证规则 1、邮箱 表单验证规则 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)); }, "邮箱格式不对");
2、电话验证规则 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");
3、区号验证规则 jQuery.validator.addMethod("ac", function (value, element) { var ac = /^0\d{2,3}$/; return this.optional(element) || (ac.test(value)); }, "区号如:010或0371");
4、无区号电话验证规则 jQuery.validator.addMethod("noactel", function (value, element) { var noactel = /^\d{7,8}$/; return this.optional(element) || (noactel.test(value)); }, "电话格式如:68787027");
5、手机验证规则 jQuery.validator.addMethod("mobile", function (value, element) { var mobile = /^1[3|4|5|7|8]\d{9}$/; return this.optional(element) || (mobile.test(value)); }, "手机格式不对");
6、邮箱或手机验证规则 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)); }, "格式不对");
7、电话或手机验证规则 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)); }, "格式不对");
8、年龄 表单验证规则 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$/
9、传真 jQuery.validator.addMethod("fax",function(value,element){ var fax = /^(\d{3,4})?[-]?\d{7,8}$/; return this.optional(element) || (fax.test(value)); },"传真格式如:0371-68787027");
10验证当前值和目标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; },"不能和原始密码相同");
11、大于指定数 jQuery.validator.addMethod("gt",function(value, element){ var returnVal = false; var gt = $(element).data("gt"); if(value > gt && value != ""){ returnVal = true; } return returnVal; },"不能小于0 或空");
12、汉字 jQuery.validator.addMethod("chinese", function (value, element) { var chinese = /^[\u4E00-\u9FFF]+$/; return this.optional(element) || (chinese.test(value)); }, "格式不对");
13、指定数字的整数倍 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; }, "必须是发布赏金的整数倍");
14、身份证 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)); }, "格式不对"); 15、字符验证 jQuery.validator.addMethod("stringCheck", function(value, element) { return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value); }, "只能包括中文字、英文字母、数字和下划线"); ------------------------------------------------------------ 16、中文字两个字节 jQuery.validator.addMethod("byteRangeLength", 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[0] && length <= param[1] ); }, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)"); ------------------------------------------------------------
------------------------------------------------------------ 17. 联系电话(手机/电话皆可)验证 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)); }, "请正确填写您的邮政编码");
原文链接:https://www.afxvip.com/3289.html,转载请注明出处。
评论0