jQuery验证插件自定义方法消息不切换

时间:2014-07-29 18:31:44

标签: jquery jquery-validate

我创建了以下规则:

 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");

并在validate构造函数中使用以下命令启用它:

  password: {
                required: true,
                minlength: 6,
                onespecial: true
            },

检查用户是否在其PW中输入了一个特殊字符或数字。如果我输入6个字母都是字母,它会抛出消息"您的密码必须包含一个数字或特殊字符"。如果我然后添加一个特殊的字符或数字,错误消息不会消失,即使我单击提交,表单验证就好了。如果我输入一个带有数字或特殊字符的6位数密码,则不会出现错误消息。任何人都知道为什么即使密码通过验证后该消息仍然存在,以及我如何消除该消息?

编辑:这是表单的HTML:

 <form action="/" id="activateForm" method="post">      
 <div>
 <input type="text" name="first_name" class="form-input" placeholder="First name" onfocus="this.placeholder=&#039;&#039;" onblur="this.placeholder=&#039;First name&#039;" id="FirstName" />
 </div>     
 <div>
 <input type="text" name="last_name" class="form-input" placeholder="Last name" onfocus="this.placeholder=&#039;&#039;" onblur="this.placeholder=&#039;Last name&#039;" id="LastName" />
 </div>
    <div>
 <input type="password" name="password" class="form-input" placeholder="Password" onfocus="this.placeholder=&#039;&#039;" onblur="this.placeholder=&#039;Password&#039;" id="Password" />
 </div>
 <div>
 <input type="password" name="pass_confirm" class="form-input" placeholder="Confirm password" onfocus="this.placeholder=&#039;&#039;" onblur="this.placeholder=&#039;Confirm password&#039;" id="PassConfirm" />
 </div>     
 <div><input type="hidden" name="key" value="9480be862a441eba57f1904cbc3a78ce92989b0e" id="Key" />
 </div>       
 <input type="submit" value="Finish" class="button button-blue" />    
 </form>

这里是JS:

 $(document).ready(function() {
    // validate signup form on keyup and submit
    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: {
                equalTo: "#Password"
            },
            position: "required",
            category: "required"

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

});

1 个答案:

答案 0 :(得分:1)

您发布的代码工作正常。确保代码的其余部分遵循有效的工作格式,例如下面的示例。

DEMO:http://jsfiddle.net/LCAf7/

<强>的jQuery

$(document).ready(function () {

    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");


    $('#myform').validate({
        rules: {
            password: {
                required: true,
                minlength: 6,
                onespecial: true
            }
        }
    });

});

<强> HTML

<form id="myform">
    <input type="text" name="password" />
    <input type="submit" />
</form>

修改

使用OP中的更新代码以及jQuery 1.9.1和Validate插件版本1.11.1,所有内容仍然可以正常工作:

http://jsfiddle.net/LCAf7/2/