富贵资源网 Design By www.hznty.com


jQuery:validate添加自定义验证

jQuery.validator.addMethod添加自定义的验证规则

addMethod:name, method, message

简单实例:单个验证的添加

复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>validate.js拓展验证</title>
<script src="/UploadFiles/2021-04-02/jquery.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.validate.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/validate.expand.js"></head>
<body>
<form action=""  method="get" id="tinyphp">
<input type="text" value="" name="isZipCode" />
<input type="submit" value="提交" />
</form>
<script type="text/javascript">
$("#tinyphp").validate({
    // 添加验证规则
    rules: {
        isZipCode: {    //验证邮箱
            isZipCode: true
        }
    }
}); 
    </script>
</body>
</html>

validate.expand.js

复制代码 代码如下:
jQuery.validator.addMethod("isZipCode", function(value, element) {  
    var tel = /^[0-9]{6}$/;
    return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码");

添加多个验证方法
复制代码 代码如下:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>validate.js拓展验证</title>
<script src="/UploadFiles/2021-04-02/jquery.min.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery.validate.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/validate.expand.js"></head>
<body>
<form action=""  method="get" id="tinyphp">
邮编:<input type="text" value="" name="isZipCode" /><br /><br />

名字:<input type="text" value="" name="userName" />
<input type="submit" value="提交" />
</form>
<script type="text/javascript">
$("#tinyphp").validate({
    // 添加验证规则
    rules: {
        isZipCode: {    //验证邮箱
            isZipCode: true
        },
        userName:{
            required: true,
            userName: true,
            rangelength: [5,10]   
        }
    },

    //重设提示信息,可省略
    messages:{
        userName: {
            required: "请填写用户名",
            rangelength: "用户名必须在5-10个字符之间"
        }      

    }
}); 
    </script>
</body>
</html>
 

 validate.expand.js

 复制代码 代码如下:
 jQuery.validator.addMethod("userName", function(value, element) {
    return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
}, "用户名必须在5-10个字符之间");  

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

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com