jQuery.validate自定义验证

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head>  
  5.     <meta name="viewport" content="width=device-width" />  
  6.     <title>Index</title>  
  7.     <link href="~/Css/css.css" rel="stylesheet" />  
  8.     <script src="~/Js/jquery.min.js"></script>  
  9.     <script src="~/Js/jquery.validate.min.js"></script>  
  10.     <script src="~/Js/jquery.validate.extend.js"></script>  
  11.     <script>  
  12.         $(document).ready(function () {  
  13.             $("#myform").validate({  
  14.                 rules: {  
  15.                     username: { required: true, minlength: 2 },  
  16.                     password: { required: true, minlength: 6, maxlength: 16 },  
  17.                     repassword: { required: true, equalTo: "#password" },  
  18.                     amt: { required: true, isAmount: true },  
  19.                     idcard: { required: true, isIdCardNo: true }  
  20.                 },  
  21.                 messages: {  
  22.                     username: {  
  23.                         required: "用户名不能为空",  
  24.                         minlength: "用户名的最小长度为2"  
  25.                     },  
  26.                     password: {  
  27.                         required: "密码不能为空",  
  28.                         minlength: "密码长度不能少于6个字符",  
  29.                         maxlength: "密码长度不能超过16个字符"  
  30.                     },  
  31.                     repassword: {  
  32.                         required: "确认密码不能为空",  
  33.                         equalTo: "确认密码和密码不一致"  
  34.                     },  
  35.                     amt: {  
  36.                         required: "金额不能为空",  
  37.                         isAmount: "交易金额必须大于0,且最多有两位小数"  
  38.                     },  
  39.                     idcard: {  
  40.                         required: "身份证不能为空",  
  41.                         isIdCardNo: "身份证号码错误"  
  42.                     }  
  43.                 }  
  44.             });  
  45.         });  
  46.     </script>  
  47. </head>  
  48. <body>  
  49.     <div>   
  50.         <form id="myform" method="post" action="">  
  51.             <fieldset>  
  52.                 <legend>jquery-validate表单校验验证</legend>  
  53.                 <div class="item">  
  54.                     <label for="username" class="item-label">用户名:</label>  
  55.                     <input type="text" id="username" name="username" class="item-text" placeholder="设置用户名">  
  56.                 </div>  
  57.                 <div class="item">  
  58.                     <label for="password" class="item-label">密码:</label>  
  59.                     <input type="password" id="password" name="password" class="item-text" placeholder="设置密码">  
  60.                 </div>  
  61.                 <div class="item">  
  62.                     <label for="password" class="item-label">确认密码:</label>  
  63.                     <input type="password" name="repassword" class="item-text" placeholder="设置确认密码">  
  64.                 </div>  
  65.                 <div class="item">  
  66.                     <label for="amt" class="item-label">金额:</label>  
  67.                     <input type="text" id="amt" name="amt" class="item-text" placeholder="交易金额">  
  68.                 </div>  
  69.                 <div class="item">  
  70.                     <label for="idcard" class="item-label">身份证号码:</label>  
  71.                     <input type="text" id="idcard" name="idcard" class="item-text" placeholder="身份证号码">  
  72.                 </div>  
  73.                 <div class="item">  
  74.                     <input type="submit" value="提交" class="item-submit">  
  75.                 </div>  
  76.             </fieldset>  
  77.         </form>  
  78.     </div>  
  79. </body>  
  80. </html>  

[javascript]  view plain  copy
  1. /// <reference path="jquery.js" />  
  2. // jquery.validate.extend.js  
  3. $.validator.setDefaults({  
  4.     onkeyup: null,  
  5.     success: function (label) {  
  6.         label.text('').addClass('valid');  
  7.     },  
  8.     onfocusin: function (element) {  
  9.         this.lastActive = element;  
  10.         $(element).addClass('highlight');  
  11.         if (this.settings.focusCleanup) {  
  12.             if (this.settings.unhighlight) {  
  13.                 this.settings.unhighlight.call(this, element, this.settings.errorClass, this.settings.validClass);  
  14.             }  
  15.             this.hideThese(this.errorsFor(element));  
  16.         }  
  17.     },  
  18.     onfocusout: function (element) {  
  19.         $(element).parent().children(".tip").remove();  
  20.         $(element).removeClass('highlight');  
  21.         this.element(element);  
  22.     }  
  23. });  
  24.   
  25. /***************************************************************** 
  26.                   jQuery Validate扩展验证方法        
  27. *****************************************************************/  
  28. // 判断整数value是否等于0   
  29. jQuery.validator.addMethod("isIntEqZero"function (value, element) {  
  30.     value = parseInt(value);  
  31.     return this.optional(element) || value == 0;  
  32. }, "整数必须为0");  
  33.   
  34.   
  35. // 判断整数value是否大于0  
  36. jQuery.validator.addMethod("isIntGtZero"function (value, element) {  
  37.     value = parseInt(value);  
  38.     return this.optional(element) || value > 0;  
  39. }, "整数必须大于0");  
  40.   
  41.   
  42. // 判断整数value是否大于或等于0  
  43. jQuery.validator.addMethod("isIntGteZero"function (value, element) {  
  44.     value = parseInt(value);  
  45.     return this.optional(element) || value >= 0;  
  46. }, "整数必须大于或等于0");  
  47.   
  48.   
  49. // 判断整数value是否不等于0   
  50. jQuery.validator.addMethod("isIntNEqZero"function (value, element) {  
  51.     value = parseInt(value);  
  52.     return this.optional(element) || value != 0;  
  53. }, "整数必须不等于0");  
  54.   
  55.   
  56. // 判断整数value是否小于0   
  57. jQuery.validator.addMethod("isIntLtZero"function (value, element) {  
  58.     value = parseInt(value);  
  59.     return this.optional(element) || value < 0;  
  60. }, "整数必须小于0");  
  61.   
  62.   
  63. // 判断整数value是否小于或等于0   
  64. jQuery.validator.addMethod("isIntLteZero"function (value, element) {  
  65.     value = parseInt(value);  
  66.     return this.optional(element) || value <= 0;  
  67. }, "整数必须小于或等于0");  
  68.   
  69.   
  70. // 判断浮点数value是否等于0   
  71. jQuery.validator.addMethod("isFloatEqZero"function (value, element) {  
  72.     value = parseFloat(value);  
  73.     return this.optional(element) || value == 0;  
  74. }, "浮点数必须为0");  
  75.   
  76.   
  77. // 判断浮点数value是否大于0  
  78. jQuery.validator.addMethod("isFloatGtZero"function (value, element) {  
  79.     value = parseFloat(value);  
  80.     return this.optional(element) || value > 0;  
  81. }, "浮点数必须大于0");  
  82.   
  83.   
  84. // 判断浮点数value是否大于或等于0  
  85. jQuery.validator.addMethod("isFloatGteZero"function (value, element) {  
  86.     value = parseFloat(value);  
  87.     return this.optional(element) || value >= 0;  
  88. }, "浮点数必须大于或等于0");  
  89.   
  90.   
  91. // 判断浮点数value是否不等于0   
  92. jQuery.validator.addMethod("isFloatNEqZero"function (value, element) {  
  93.     value = parseFloat(value);  
  94.     return this.optional(element) || value != 0;  
  95. }, "浮点数必须不等于0");  
  96.   
  97.   
  98. // 判断浮点数value是否小于0   
  99. jQuery.validator.addMethod("isFloatLtZero"function (value, element) {  
  100.     value = parseFloat(value);  
  101.     return this.optional(element) || value < 0;  
  102. }, "浮点数必须小于0");  
  103.   
  104.   
  105. // 判断浮点数value是否小于或等于0   
  106. jQuery.validator.addMethod("isFloatLteZero"function (value, element) {  
  107.     value = parseFloat(value);  
  108.     return this.optional(element) || value <= 0;  
  109. }, "浮点数必须小于或等于0");  
  110.   
  111.   
  112. // 判断浮点型    
  113. jQuery.validator.addMethod("isFloat"function (value, element) {  
  114.     return this.optional(element) || /^[-\+]?\d+(\.\d+)?$/.test(value);  
  115. }, "只能包含数字、小数点等字符");  
  116.   
  117.   
  118. // 匹配integer  
  119. jQuery.validator.addMethod("isInteger"function (value, element) {  
  120.     return this.optional(element) || (/^[-\+]?\d+$/.test(value) && parseInt(value) >= 0);  
  121. }, "匹配integer");  
  122.   
  123.   
  124. // 判断数值类型,包括整数和浮点数  
  125. jQuery.validator.addMethod("isNumber"function (value, element) {  
  126.     return this.optional(element) || /^[-\+]?\d+$/.test(value) || /^[-\+]?\d+(\.\d+)?$/.test(value);  
  127. }, "匹配数值类型,包括整数和浮点数");  
  128.   
  129.   
  130. // 只能输入[0-9]数字  
  131. jQuery.validator.addMethod("isDigits"function (value, element) {  
  132.     return this.optional(element) || /^\d+$/.test(value);  
  133. }, "只能输入0-9数字");  
  134.   
  135.   
  136. // 判断中文字符   
  137. jQuery.validator.addMethod("isChinese"function (value, element) {  
  138.     return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);  
  139. }, "只能包含中文字符。");  
  140.   
  141.   
  142. // 判断英文字符   
  143. jQuery.validator.addMethod("isEnglish"function (value, element) {  
  144.     return this.optional(element) || /^[A-Za-z]+$/.test(value);  
  145. }, "只能包含英文字符。");  
  146.   
  147.   
  148. // 手机号码验证      
  149. jQuery.validator.addMethod("isMobile"function (value, element) {  
  150.     var length = value.length;  
  151.     return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(value));  
  152. }, "请正确填写您的手机号码。");  
  153.   
  154.   
  155. // 电话号码验证      
  156. jQuery.validator.addMethod("isPhone"function (value, element) {  
  157.     var tel = /^(\d{3,4}-?)?\d{7,9}$/g;  
  158.     return this.optional(element) || (tel.test(value));  
  159. }, "请正确填写您的电话号码。");  
  160.   
  161.   
  162. // 联系电话(手机/电话皆可)验证     
  163. jQuery.validator.addMethod("isTel"function (value, element) {  
  164.     var length = value.length;  
  165.     var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;  
  166.     var tel = /^(\d{3,4}-?)?\d{7,9}$/g;  
  167.     return this.optional(element) || tel.test(value) || (length == 11 && mobile.test(value));  
  168. }, "请正确填写您的联系方式");  
  169.   
  170.   
  171. // 匹配qq        
  172. jQuery.validator.addMethod("isQq"function (value, element) {  
  173.     return this.optional(element) || /^[1-9]\d{4,12}$/;  
  174. }, "匹配QQ");  
  175.   
  176.   
  177. // 邮政编码验证      
  178. jQuery.validator.addMethod("isZipCode"function (value, element) {  
  179.     var zip = /^[0-9]{6}$/;  
  180.     return this.optional(element) || (zip.test(value));  
  181. }, "请正确填写您的邮政编码。");  
  182.   
  183.   
  184. // 匹配密码,以字母开头,长度在6-12之间,只能包含字符、数字和下划线。        
  185. jQuery.validator.addMethod("isPwd"function (value, element) {  
  186.     return this.optional(element) || /^[a-zA-Z]\\w{6,12}$/.test(value);  
  187. }, "以字母开头,长度在6-12之间,只能包含字符、数字和下划线。");  
  188.   
  189.   
  190. // 身份证号码验证  
  191. jQuery.validator.addMethod("isIdCardNo"function (value, element) {  
  192.     //var idCard = /^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/;     
  193.     return this.optional(element) || isIdCardNo(value);  
  194. }, "请输入正确的身份证号码。");  
  195.   
  196.   
  197. // IP地址验证     
  198. jQuery.validator.addMethod("ip"function (value, element) {  
  199.     return this.optional(element) || /^(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))$/.test(value);  
  200. }, "请填写正确的IP地址。");  
  201.   
  202.   
  203. // 字符验证,只能包含中文、英文、数字、下划线等字符。      
  204. jQuery.validator.addMethod("stringCheck"function (value, element) {  
  205.     return this.optional(element) || /^[a-zA-Z0-9\u4e00-\u9fa5-_]+$/.test(value);  
  206. }, "只能包含中文、英文、数字、下划线等字符");  
  207.   
  208.   
  209. // 匹配english    
  210. jQuery.validator.addMethod("isEnglish"function (value, element) {  
  211.     return this.optional(element) || /^[A-Za-z]+$/.test(value);  
  212. }, "匹配english");  
  213.   
  214.   
  215. // 匹配汉字    
  216. jQuery.validator.addMethod("isChinese"function (value, element) {  
  217.     return this.optional(element) || /^[\u4e00-\u9fa5]+$/.test(value);  
  218. }, "匹配汉字");  
  219.   
  220.   
  221. // 匹配中文(包括汉字和字符)   
  222. jQuery.validator.addMethod("isChineseChar"function (value, element) {  
  223.     return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);  
  224. }, "匹配中文(包括汉字和字符) ");  
  225.   
  226.   
  227. // 判断是否为合法字符(a-zA-Z0-9-_)  
  228. jQuery.validator.addMethod("isRightfulString"function (value, element) {  
  229.     return this.optional(element) || /^[A-Za-z0-9_-]+$/.test(value);  
  230. }, "判断是否为合法字符(a-zA-Z0-9-_)");  
  231.   
  232.   
  233. // 判断是否包含中英文特殊字符,除英文"-_"字符外  
  234. jQuery.validator.addMethod("isContainsSpecialChar"function (value, element) {  
  235.     var reg = RegExp(/[(\ )(\`)(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)( )( )(\+)(\=)(\|)(\{)(\})(\')(\:)(\;)(\')(',)(
    )(
    )(\.)(\<)(\>)(\/)(\?)(\~)(\!)(\@)(\#)(\¥)(\%)(\…)(\&)(\*)(\()(\))(\—)(\+)(\|)(\{)(\})(\【)(\】)(\‘)(\;)(\:)(\”)(\“)(\’)(\。)(\,)(\、)(\?)]+/);  
  236.     return this.optional(element) || !reg.test(value);  
  237. }, "含有中英文特殊字符");  
  238.   
  239.   
  240. // 判断是否金额 小数点后两位  
  241. jQuery.validator.addMethod(  
  242.     "isAmount",  
  243.     function (value, element) {  
  244.         return value && /^\d*\.?\d{0,2}$/.test(value);  
  245.     },  
  246.     "金额必须大于0且小数位数不超过2位"  
  247. );  
  248.   
  249. //身份证号码的验证规则  
  250. function isIdCardNo(num) {  
  251.     //if (isNaN(num)) {alert("输入的不是数字!"); return false;}   
  252.     var len = num.length, re;  
  253.     if (len == 15)  
  254.         re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{2})(\w)$/);  
  255.     else if (len == 18)  
  256.         re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);  
  257.     else {  
  258.         //alert("输入的数字位数不对。");   
  259.         return false;  
  260.     }  
  261.     var a = num.match(re);  
  262.     if (a != null) {  
  263.         if (len == 15) {  
  264.             var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);  
  265.             var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];  
  266.         }  
  267.         else {  
  268.             var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);  
  269.             var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];  
  270.         }  
  271.         if (!B) {  
  272.             //alert("输入的身份证号 "+ a[0] +" 里出生日期不对。");   
  273.             return false;  
  274.         }  
  275.     }  
  276.     if (!re.test(num)) {  
  277.         //alert("身份证最后一位只能是数字和字母。");  
  278.         return false;  
  279.     }  
  280.     return true;  
  281. }  

[css]  view plain  copy
  1. body {  
  2.     font-family: Microsoft Yahei;  
  3.     font-size15px;  
  4. }  
  5.   
  6. fieldset {  
  7.     width680px;  
  8. }  
  9.   
  10. legend {  
  11.     margin-left8px;  
  12. }  
  13.   
  14. .item {  
  15.     height56px;  
  16.     line-height36px;  
  17.     margin10px;  
  18. }  
  19.   
  20. label {  
  21.     floatleft;  
  22. }  
  23.   
  24. .item .item-label {  
  25.     floatleft;  
  26.     width80px;  
  27.     text-alignright;  
  28. }  
  29.   
  30. .item-text {  
  31.     floatleft;  
  32.     width244px;  
  33.     height16px;  
  34.     padding9px 25px 9px 5px;  
  35.     margin-left10px;  
  36.     border1px solid #ccc;  
  37.     overflowhidden;  
  38. }  
  39.   
  40. .item-submit {  
  41.     margin-left88px;  
  42. }  
  43.   
  44. input.error {  
  45.     border1px solid #E6594E;  
  46. }  
  47.   
  48. input.highlight {  
  49.     border1px solid #7abd54;  
  50. }  
  51.   
  52. label.error, label.tip {  
  53.     floatleft;  
  54.     font-size14px;  
  55.     text-alignleft;  
  56.     margin-left5px;  
  57.     padding-left20px;  
  58.     colorred;  
  59.     backgroundurl('/images/error.png'no-repeat left center;  
  60. }  
  61. label.valid {  
  62.     backgroundurl('/images/right.png'no-repeat left center;  

  1. }  

方法的使用如下:把“ stringCheck”直接放进就OK了。
	rules: {
            txtProductName: {
                required: true,
                stringCheck: true
            },

jQuery.validator.addMethod("stringCheck", function (value, element) {
    //return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
    if ($("#txtProductName").val().length < 10) {
        return false;
    }
    else {
        return true;
    }
}, "请输入适合的长度");

//验证
function validateFrom() {
    $("#form1").validate({
        rules: {
            txtProductName: {
                required: true,
                stringCheck: true
            },
            txtMarketPrice: {
                required: true,
                number: true
            },
            txtIntegral: {
                required: true,
                digits: true
            }
        },
        messages: {
            txtProductName: {
                required: "请输入礼品名称"
            },
            txtMarketPrice: {
                required: "请输入市场价"
            },
            txtIntegral: {
                required: "请输入兑换积分"
            }
        }
        , success: function (label) {
            label.html("&nbsp;").attr("class", "success").siblings("label").remove();
        },
        errorPlacement: function (error, element) {
            $(element).next("span").find(".success").remove();
            error.appendTo(element.next("span"));
        }
    });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值