Jquery验证器addmethod次要问题

时间:2017-04-23 10:30:45

标签: javascript jquery

我是jquery验证器的新手,我试图使用正则表达式来检查是否包含数字,大写字母和特殊字符。我有以下html

<div class="wrapper wrapper-content animated fadeInRight">
<div class="row white-bg spacepad">
    <div id="keypass">
        <form method="POST" class="form-horizontal" id="keyform" name="keypasser">
            <div class="form-group"><label class="col-sm-2 control-label">Decryption Key For EPins</label>
                <div class="col-sm-10"><input type="text" required="required" class="form-control" name="keypassx" /></div>
            </div>
            <div class="form-group"><label class="col-sm-2 control-label">Confirm Decryption Key</label>
                <div class="col-sm-10"><input type="text" required="required" class="form-control" name="keypass2" /></div>
            </div>
            <input type="submit" class="btn btn-primary btn-rounded btn-block" value="Submit" />
        </form>
    </div>
</div>

和以下jquery,当我删除addmethod它工作,请问我做错了什么

<script type="text/javascript">
$(function() {
// Initialize form validation on the registration form.
// It has the name attribute "registration"
$("form[name='keypasser']").validate({
    // Specify validation rules
    rules: {
        keypassx: {
            required: true,
            minlength: 8,
            passchecker: true,
        },
        keypass2: {
            required: true,
            equalTo: "#keypassx",
        }
    },
    // Specify validation error messages
    messages: {
        keypassx: {
            required: "Epins password required",
            minlength: "Minimum length allowed is 8 characters",
            passchecker: "Password should contain numbers, special characters and one uppercase",
        },
        keypass2: {
            required: "Confirmation password requied",
            equalTo: "Password does not match Epins Password",
        }
    },

    $.validator.addMethod("passchecker",
        function(value, element) {
             return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value);
    });

    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) {
        form.submit();
    }
});

});
</script>

请帮忙

2 个答案:

答案 0 :(得分:0)

请尝试使用正则表达式

var patt = new RegExp(“e”); var res = patt.test(str);

您可以直接使用正则表达式。请创建对象并尝试。

答案 1 :(得分:0)

addMethod函数接受三个参数检查docs

$.validator.addMethod("passchecker",
        function(value, element) {
             if(/^[A-Za-z0-9\d=!\-@._*]*$/.test(value)) {
                return true;
             } else {
                return false;
             }
    }, "Your validation message goes here");