将正则表达式添加到 jquery.validate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13785529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:57:19  来源:igfitidea点击:

add regex to jquery.validate

jqueryjquery-plugins

提问by isaac weathers

Need to add a regex check to my jquery.validate script.

需要向我的 jquery.validate 脚本添加正则表达式检查。

I have this:

我有这个:

$().ready(function() {
  $("#myBlahForm").validate({

    rules: {
        someName: {
            required: true,
            minlength: 5,
            maxlength: 8,
        },
        somePassword: {
            required: true,
                            minlength: 5,
            maxlength: 8,
        },
        someConfirmPassword: {
            required: true,
            equalTo: "#somePassword"
        },
    },
    messages: {
        someName:  {
            required: "Please create your username",
        },
        somePassword: {
            required: "Please create a password",
        },
        someConfirmPassword: {
            required: "Please re-enter your password",
            equalTo:  "Passwords must match"
        },
    }
});

});

});

And I would like to check for specific characters that are allowed in the DB for passwords when the user enters their data here:

当用户在此处输入数据时,我想检查数据库中允许的特定字符以获取密码:

    <label for="someName">Username</label>
    <input type="text" id="someName" name="someName" placeholder="Create a Username"/>

    <label for="somePassword">Password</label>
    <input type="password" id="somePassword" name="somePassword" placeholder="Create a Password"/>

    <label for="someConfirmPassword">Confirm Password </label>
    <input type="password" id="someConfirmPassword" name="someConfirmPassword" placeholder="Verify your Password"/>

What's the best way to add a regex check to a required field such as a password or username using jquery.validate plugin from bassisstance: http://docs.jquery.com/Plugins/Validation

使用bassisstance的jquery.validate插件将正则表达式检查添加到必填字段(例如密码或用户名)的最佳方法是什么:http://docs.jquery.com/Plugins/Validation

Basically, I need to make sure that the password and usernames they create only have 0-9, letters, and a couple of special characters.

基本上,我需要确保他们创建的密码和用户名只有 0-9、字母和几个特殊字符。

回答by Sudhir Bastakoti

well, you could add your custom validation method using addMethod, like

好吧,您可以使用添加自定义验证方法addMethod,例如

$.validator.addMethod("regx", function(value, element, regexpr) {          
    return regexpr.test(value);
}, "Please enter a valid pasword.");

And ,

和 ,

...
$("#myBlahForm").validate({

    rules: {
        somePassword: {
            required: true ,
            //change regexp to suit your needs
            regx: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}$/,
            minlength: 5,
            maxlength: 8
        }
    }

回答by Ohgodwhy

required: function(element){
    return /[a-zA-Z0-9]/.test($('#someConfirmPassword').val());
}

Above expression will return true if regexp passes, false if it doesn't. Change it to fit your needs.

如果正则表达式通过,则上面的表达式将返回 true,否则返回 false。更改它以满足您的需求。