简体   繁体   English

jQuery验证 - 密码必须包含大写和数字

[英]jQuery Validation - password MUST contain uppercase and number

So in my registration form I have this field: 所以在我的注册表格中我有这个字段:

 <div class="form-group">
    <label for="RegisterModel_Password">Password</label>
    <input type="password" id="RegisterModel_Password" 
           name="RegisterModel.Password" class="form-control" 
           required="required" minlength="8"/>
</div>

As you see, I'm using jQuery validation attributes to ensure that the password includes at least 8 characters. 如您所见,我正在使用jQuery验证属性来确保密码至少包含8个字符。 So, I want to check if password contains uppercase and number, if not, field is not valid. 所以,我想检查密码是否包含大写和数字,如果没有,则字段无效。 I downloaded additional method for jQuery Validation plugin named " pattern " and added her in head tag. 我下载了名为“ pattern ”的jQuery Validation插件的附加方法,并将她添加到head标签中。

I tried to do this as follows but it didn't worked. 我尝试按照以下方式执行此操作,但它没有奏效。

$("#formRegister").validate({
    rules: {
        RegisterModel_Password: {
            pattern: /^[a-zA-Z][0-9]/
        }
    }
});

I assume that the pattern is wrong, but I'm not sure whether the use is correct. 我认为模式是错误的,但我不确定使用是否正确。 Thank you for your help. 谢谢您的帮助。

Chains of regular expressions are too hard for me ( I have never tried to learn them lol ). 正则表达的链对我来说太难了(我从来没有试过去学习它们)。 So here is my solution: 所以这是我的解决方案:

jQuery.validator.addMethod("passwordCheck",
        function(value, element, param) {
            if (this.optional(element)) {
                return true;
            } else if (!/[A-Z]/.test(value)) {
                return false;
            } else if (!/[a-z]/.test(value)) {
                return false;
            } else if (!/[0-9]/.test(value)) {
                return false;
            }

            return true;
        },
        "error msg here");

And simply I use it like a attribute: 简单地说,我就像一个属性一样使用它:

<input type="password" id="RegisterModel_Password" 
name="RegisterModel.Password" 
class="form-control" 
required="required" minlength="8" 
passwordCheck="passwordCheck"/>

Thanks for your answers. 谢谢你的回答。

You can add your custom validation using $.validator.addMethod() like: 您可以使用$.validator.addMethod()添加自定义验证,如:

$.validator.addMethod("validation_name", function(value) {
    // at least 1 number and at least 1 character
    [^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 密码的正则表达式必须至少包含八个字符,至少一个数字以及大小写字母和特殊字符 - Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters Jquery验证密码强度大写和小写 - Jquery validation password strength uppercase and lowercase 如何使用大写和数字的正则表达式验证匹配密码并确认密码 - How to match password and confirm password with regex validation for uppercase and number Javascript验证-最小/最大字符数,并且必须包含数字 - Javascript validation - Min/Max number of characters AND must contain number 文本框必须在没有正则表达式的javascript上包含数字或字母验证 - textbox must contain number or letter validation on javascript without regex 密码正则表达式 - 最少 12 个字符,应包含 1 个大写、小写、数字和特殊字符,并且不应包含超过 2 个重复字符 - Password RegEx - Minimum 12 char, Should contain 1 Uppercase & Lowercase & Number & Special Char and should not contain more than 2 repeated char jQuery允许数字和文本(大写) - Jquery Allow number and text (Uppercase) 如何使用 2 个大写字母和数字创建验证? - How to create validation with 2 uppercase letter and number? 密码必须包含以下任一条件 - Password must contain any one of the following criteria 验证密码 - 必须包含 2 个大写字母 - Validate password - must contain 2 capital letters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM