1

我正在使用 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>
4

2 回答 2

4

您没有从自定义方法中收到消息,因为您正在覆盖所有错误消息passwordpass_confirm使用messages选项...

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",  
},

演示:http: //jsfiddle.net/WZPcW/

于 2013-10-15T20:06:36.570 回答
0

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

对我来说,你正在过度阅读消息,当你做这样的事情时:

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

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

编辑:哇,我又没有看到答案:/ Sparky 写了所有关于你的问题。

于 2013-10-15T20:15:38.623 回答