jQuery 验证插件 - 密码检查 - 最低要求 - 正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18746234/
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
jQuery Validate plugin - password check - minimum requirements - Regex
提问by bayerphi
I've got a little problem with my password-checker.
我的密码检查器有点问题。
There's got a registration form with some fields. I use jQuery Validate plugin to validate user-inputs.
有一个包含一些字段的注册表。我使用 jQuery Validate 插件来验证用户输入。
It all works except the password-validation:
除了密码验证之外,一切都有效:
The password should meet some minimum requirements:
密码应满足一些最低要求:
- minimum length: 8 -> I just use 'minlength: 8'
- at least one lower-case character
- at least one digit
- Allowed Characters: A-Z a-z 0-9 @ * _ - . !
- 最小长度:8 -> 我只使用 'minlength: 8'
- 至少一个小写字符
- 至少一位
- 允许的字符:AZ az 0-9 @ * _ - 。!
At the moment I use this code to validate the password:
目前我使用此代码来验证密码:
$.validator.addMethod("pwcheck",
function(value, element) {
return /^[A-Za-z0-9\d=!\-@._*]+$/.test(value);
});
This Code works for the allowed characters but not for minimum requirements.
I know that you can use for example (?=.*[a-z])
for a lower-case-requirement. But I just don't get it to work.
此代码适用于允许的字符,但不适用于最低要求。我知道您可以使用例如(?=.*[a-z])
小写要求。但我只是不让它工作。
If I add (?=.*[a-z])
the whole code doesn't work anymore. I need to know how to properly add the code to the existing one.
如果我添加(?=.*[a-z])
整个代码不再起作用。我需要知道如何将代码正确添加到现有代码中。
Thank you for your answers!
谢谢您的回答!
This is the complete code
这是完整的代码
<script>
$(function() {
$("#regform").validate({
rules: {
forename: {
required: true
},
surname: {
required: true
},
username: {
required: true
},
password: {
required: true,
pwcheck: true,
minlength: 8
},
password2: {
required: true,
equalTo: "#password"
},
mail1: {
required: true,
email: true
},
mail2: {
required: true,
equalTo: "#mail1"
}
},
messages: {
forename: {
required: "Vornamen angeben"
},
surname: {
required: "Nachnamen angeben"
},
username: {
required: "Usernamen angeben"
},
password: {
required: "Passwort angeben",
pwcheck: "Das Passwort entspricht nicht den Kriterien!",
minlength: "Das Passwort entspricht nicht den Kriterien!"
},
password2: {
required: "Passwort wiederholen",
equalTo: "Die Passw?rter stimmen nicht überein"
},
mail1: {
required: "Mail-Adresse angeben",
email: "ungültiges Mail-Format"
},
mail2: {
required: "Mail-Adresse wiederholen",
equalTo: "Die Mail-Adressen stimmen nicht überein"
}
}
});
$.validator.addMethod("pwcheck",
function(value, element) {
return /^[A-Za-z0-9\d=!\-@._*]+$/.test(value);
});
});
</script>
回答by Bergi
If I add
(?=.*[a-z])
the whole code doesn't work anymore.
如果我添加
(?=.*[a-z])
整个代码不再起作用。
Add it here:
在此处添加:
/^(?=.*[a-z])[A-Za-z0-9\d=!\-@._*]+$/
However, it's much easier to do this without a lookahead:
但是,在没有前瞻的情况下更容易做到这一点:
$.validator.addMethod("pwcheck", function(value) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) // consists of only these
&& /[a-z]/.test(value) // has a lowercase letter
&& /\d/.test(value) // has a digit
});
回答by Anon
Well you can use {8,} instead of "+" for a minimum of 8 chars with no maximum or better yet a {8, 20} for a minimum of 8 and a maximum of 20.
好吧,您可以使用 {8,} 而不是“+”来表示最少 8 个字符,没有最大值或更好的是 {8, 20} 表示最小值为 8,最大值为 20。
Really though I don't see the value in trying to squeeze all of your validation into a single regexp. If you break it up it makes it much easier to maintain, less bug prone, and it enables you to report back to the user the specific reason WHY the password failed instead of the entire requirement.
真的,尽管我没有看到尝试将所有验证压缩到单个正则表达式中的价值。如果您将其分解,它会更容易维护,更不容易出错,并且您可以向用户报告密码失败的具体原因,而不是整个要求。
You could break it up into a few checks
你可以把它分解成几张支票
//proper length
value.length >= 8
//only allowed characters
/^[A-Za-z0-9\d=!\-@._*]+$/.test(value)
//has a digit
/\d/.test(value)
//has a lowercase letter
/[a-z]/.test(value)
I'm not familiar with the jQuery Validation plugin, but I assume you could then return helpful a helpful message for each test that failed.
我不熟悉 jQuery 验证插件,但我假设您可以为每个失败的测试返回有用的消息。
回答by sandeep kumar
if you want to check confirm passwordand minimum charactervalidation, then you can use
如果要检查确认密码和最小字符验证,则可以使用
<input type="password" id="password" name="password" class="validate[required,minSize[8]]"/>
<input type="password" id="confirm_password" name="confirm_password" class="validate[required,equals[password]]"/>
回答by David Castro
Password validation can use several rules, for example:
密码验证可以使用多种规则,例如:
var _validatePassword = function (validateUserNameRules, inputModel)
{
//bolean parameter validateUserNameRules -> true/false
//this method recive a model like this:
//inputModel.userName -> string
//inputModel.password -> string
//inputModel.password2 -> String
var ResultModel = {
ResultId: 1, //1 success
Message: "Password is correct."
};
if (validateUserNameRules && inputModel.userName == "") {
ResultModel.ResultId = 2;
ResultModel.Message = "Error: User name cannot be blank.";
return (ResultModel);
}
var re = /^\w+$/;
if (validateUserNameRules && !re.test(inputModel.userName)) {
ResultModel.ResultId = 2;
ResultModel.Message = "Error: Username must contain only letters, numbers and underscores.";
return (ResultModel);
}
if (inputModel.password != "" && inputModel.password == inputModel.password2) {
if (inputModel.password.length < 6) {
ResultModel.ResultId = 2;
ResultModel.Message = "Error: Password must contain at least six characters.";
return (ResultModel);
}
if (validateUserNameRules && inputModel.password == inputModel.userName) {
ResultModel.ResultId = 2;
ResultModel.Message = "Error: Password must be different from the Account Name.";
return (ResultModel);
}
re = /[0-9]/;
if (!re.test(inputModel.password)) {
ResultModel.ResultId = 2;
ResultModel.Message = "Error: Password must contain at least one number (0-9).";
return (ResultModel);
}
re = /[a-z]/;
if (!re.test(inputModel.password)) {
ResultModel.ResultId = 2;
ResultModel.Message = "Error: Password must contain at least one lowercase letter (a-z).";
return (ResultModel);
}
re = /[A-Z]/;
if (!re.test(inputModel.password)) {
ResultModel.ResultId = 2;
ResultModel.Message = "Error: Password must contain at least one uppercase letter (A-Z).";
return (ResultModel);
}
} else {
ResultModel.ResultId = 2;
ResultModel.Message = "Error: Please check that you've entered and confirmed your password.";
return (ResultModel);
}
return (ResultModel); //success password validation!!
};
回答by Monzur
If you use ValidationEngine then you can use this
如果您使用 ValidationEngine 那么您可以使用它
"password": {
"regex": /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{6,30}$/, // /^[A-Za-z0-9\d=!\-@._*]*$/i
"alertText": "* Invalid Password, <br/>"
+ " 1] Min 1 uppercase letter.<br/>"
+ " 2] Min 1 lowercase letter.<br/>"
+ " 3] Min 1 special character.<br/>"
+ " 4] Min 1 number."
},
Add this code in jquery.validationEngine-en.js file
将此代码添加到 jquery.validationEngine-en.js 文件中
Then use
然后使用
class="validate[required,custom[password],minSize[6],maxSize[30]]"
in Text Box class
在文本框类中