jQuery验证不出现特殊方法的自定义消息

时间:2013-10-15 16:12:33

标签: jquery jquery-validate

我正在使用jQuery Validator插件,它一直很好用。我刚刚添加了另一个自定义方法来验证密码是否包含一个数字或一个特殊字符,这也很有用。问题是,当该条件失败时(它们只输入6个字符的文本)它永远不会抛出错误消息“您的密码必须包含至少一个数字或特殊字符”。谁知道为什么会这样?代码如下:

jQuery.validator.addMethod("onespecial", function(value, element){
        var pattern = /^(?=.{6,})(?=.*[a-z])(?=.*[0-9])|(?=.*[!@#$%^&*()-+=]).*$/;

        return (pattern.test(value));

    }, "Your password must contain one number or special character");

    $("#activateForm").validate({
        rules: {
            first_name: "required",
            last_name: "required",
            password: {
                required: true,
                minlength: 6,
                onespecial: true
            },
            pass_confirm: {
                required: true,
                minlength: 6,
                equalTo: "#Password",
                onespecial: true
            },
            position: "required",
            category: "required"

        },
        messages: {
            first_name: "Please enter your first name",
            last_name: "Please enter your last name",
            password: "Please enter a valid password",
            pass_confirm: "Your passwords much match",
            position: "Please select your position",
            category: "Please select your category"
        }
    }); 

更新:使用标记

 <div id="activate-form">
<h1>Welcome. We just need a little bit more information.</h1>
  <form action="/" id="activateForm" method="post" novalidate="novalidate">     <div><input type="text" name="first_name" class="form-input" placeholder="First name" onfocus="this.placeholder=''" onblur="this.placeholder='First name'" id="FirstName"></div>        <div><input type="text" name="last_name" class="form-input" placeholder="Last name" onfocus="this.placeholder=''" onblur="this.placeholder='Last name'" id="LastName"></div>        <div><select name="category" class="form-input" id="Category"><option value="">Select category</option><option value="test">test</option><option value="test">test</option></select></div>
    <div><select name="position" class="form-input" id="Position"><option value="">Select Position</option><option value="RA">RA</option></select></div>
    <div><input type="password" name="password" class="form-input" placeholder="Password" onfocus="this.placeholder=''" onblur="this.placeholder='Password'" id="Password"></div>       <div><input type="password" name="pass_confirm" class="form-input" placeholder="Confirm password" onfocus="this.placeholder=''" onblur="this.placeholder='Confirm password'" id="PassConfirm"></div>        <div><input type="hidden" name="key" value="fff" id="Key"></div>          <input type="submit" value="Finish" class="button">     </form></div>

2 个答案:

答案 0 :(得分:3)

您没有收到自定义方法中的消息,因为您使用password覆盖了pass_confirmmessages所有错误消息选项...

messages: {
    first_name: "Please enter your first name",
    last_name: "Please enter your last name",
    password: "Please enter a valid password",  // <-- overrides all messages
    pass_confirm: "Your passwords much match",  // <-- overrides all messages
    position: "Please select your position",
    category: "Please select your category"
}

您可以为每个规则指定一条消息,然后当您没有为自定义方法指定一条消息时,将使用您在addMethod中定义的消息。

messages: {
    first_name: "Please enter your first name",
    last_name: "Please enter your last name",
    password: {
        required: "Please enter a valid password",
        minlength: "Please enter a valid password"
        // onespecial: "" // <-- message already defined within addMethod()
    },
    pass_confirm: "Your passwords much match", 
    position: "Please select your position",
    category: "Please select your category"
}

另请注意:无需复制pass_confirm字段的所有规则。因为无论什么,它必须始终与密码字段匹配,因此已满足这些条件

password: {
    required: true,
    minlength: 6,
    onespecial: true
},
pass_confirm: {
    equalTo: "#Password",  
},

DEMO:http://jsfiddle.net/WZPcW/

答案 1 :(得分:0)

请附上JsFiddle版本以重现它,因此我们可以运行您的代码并对其进行测试。

对我而言,你正在覆盖消息,当你做这样的事情时:

messages: {
    password: 
    {
        required: "Please enter a valid password",
        minlength: "Please enter a valid password"
    }
}

它应该可以工作,因为它会将消息分配给它们的验证名称,并且不应覆盖自定义方法。 但我只是在猜测。

编辑:哇,我再也没有看到答案:/ Sparky写了关于你的mestake的所有内容。