Javascript 密码 REGEX,最少 6 个字符,至少一个字母和一个数字,可能包含特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7844359/
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
Password REGEX with min 6 chars, at least one letter and one number and may contain special characters
提问by Budiawan
I need a regular expression with condition:
我需要一个带条件的正则表达式:
- min 6 characters, max 50 characters
- must contain 1 letter
- must contain 1 number
- may contain special characters like !@#$%^&*()_+
- 最少 6 个字符,最多 50 个字符
- 必须包含 1 个字母
- 必须包含 1 个数字
- 可能包含特殊字符,如 !@#$%^&*()_+
Currently I have pattern: (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,50})$
目前我有模式: (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,50})$
However it doesn't allow special characters, does anybody have a good regex for that?
但是它不允许特殊字符,有人有好的正则表达式吗?
Thanks
谢谢
回答by jfriend00
Perhaps a single regex could be used, but that makes it hard to give the user feedback for which rule they aren't following. A more traditional approach like this gives you feedback that you can use in the UI to tell the user what pwd rule is not being met:
也许可以使用单个正则表达式,但这使得很难向用户反馈他们不遵循的规则。像这样的更传统的方法为您提供反馈,您可以在 UI 中使用这些反馈来告诉用户哪些密码规则没有得到满足:
function checkPwd(str) {
if (str.length < 6) {
return("too_short");
} else if (str.length > 50) {
return("too_long");
} else if (str.search(/\d/) == -1) {
return("no_num");
} else if (str.search(/[a-zA-Z]/) == -1) {
return("no_letter");
} else if (str.search(/[^a-zA-Z0-9\!\@\#$\%\^\&\*\(\)\_\+]/) != -1) {
return("bad_char");
}
return("ok");
}
回答by Hugo
following jfriend00 answer i wrote this fiddle to test his solution with some little changes to make it more visual:
在 jfriend00 回答之后,我写了这个小提琴来测试他的解决方案,并进行一些小改动以使其更加直观:
and this is the code:
这是代码:
checkPwd = function() {
var str = document.getElementById('pass').value;
if (str.length < 6) {
alert("too_short");
return("too_short");
} else if (str.length > 50) {
alert("too_long");
return("too_long");
} else if (str.search(/\d/) == -1) {
alert("no_num");
return("no_num");
} else if (str.search(/[a-zA-Z]/) == -1) {
alert("no_letter");
return("no_letter");
} else if (str.search(/[^a-zA-Z0-9\!\@\#$\%\^\&\*\(\)\_\+\.\,\;\:]/) != -1) {
alert("bad_char");
return("bad_char");
}
alert("oukey!!");
return("ok");
}
btw, its working like a charm! ;)
顺便说一句,它的工作就像一个魅力!;)
best regards and thanks to jfriend00 of course!
最好的问候,当然要感谢 jfriend00!
回答by Albz
A more elegant and self-contained regex to match these (common) password requirements is:
匹配这些(常见)密码要求的更优雅和自包含的正则表达式是:
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d^a-zA-Z0-9].{5,50}$
The elegant touchhere is that you don't have to hard-code symbolssuch as $ @ # etc. To accept all the symbols, you are simply saying: "accept also all the not alphanumeric characters and not numbers".
The final part of the regex {5,50}
can be easily changed and corresponds to the min and max number of characters: in this specific example until 5 characters the regex returns a non match (i.e. min 6 characters are required to validate). Similarly, after 50 characters it returns a non match as well.
这里的优雅之处在于您不必对诸如 $@# 等符号进行硬编码。要接受所有符号,您只需说:“也接受所有非字母数字字符和非数字”。正则表达式的最后部分{5,50}
可以很容易地更改,并对应于最小和最大字符数:在这个特定示例中,直到 5 个字符,正则表达式返回不匹配(即需要至少 6 个字符进行验证)。同样,在 50 个字符之后,它也会返回一个不匹配的结果。
回答by Joomler
Check a password between 7 to 16 characters which contain only characters, numeric digits, underscore and first character must be a letter-
/^[A-Za-z]\w{7,14}$/
Check a password between 6 to 20 characters which contain at least one numeric digit, one uppercase, and one lowercase letter
/^(?=.\d)(?=.[a-z])(?=.*[A-Z]).{6,20}$/
Check a password between 7 to 15 characters which contain at least one numeric digit and a special character
/^(?=.[0-9])(?=.[!@#$%^&])[a-zA-Z0-9!@#$%^&]{7,15}$/
Check a password between 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character
/^(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[^a-zA-Z0-9])(?!.*\s).{8,15}$/
检查 7 到 16 个字符之间的密码,其中仅包含字符、数字、下划线和第一个字符必须是字母-
/^[A-Za-z]\w{7,14}$/
检查 6 到 20 个字符之间的密码,其中至少包含一位数字、一位大写字母和一位小写字母
/ ^(?=。\ d)(?=。[AZ])(?=。* [AZ])。{6,20} $ /
检查 7 到 15 个字符之间的密码,其中至少包含一个数字和一个特殊字符
/ ^(?=。[0-9])(?=。[!@#$%^&])[A-ZA-Z0-9!@#$%^&] {7,15} $ /
检查 8 到 15 个字符之间的密码,其中至少包含 1 个小写字母、1 个大写字母、1 个数字和 1 个特殊字符
/^(?=. \d)(?=.[az])(?=. [AZ])(?=.[^a-zA-Z0-9])(?!.*\s).{ 8,15}$/
回答by Fischermaen
I have a regex, but it's a bit tricky.
我有一个正则表达式,但它有点棘手。
^(?:(?<Numbers>[0-9]{1})|(?<Alpha>[a-zA-Z]{1})|(?<Special>[^a-zA-Z0-9]{1})){6,50}$
Let me explain it and how to check if the tested password is correct:
让我解释一下以及如何检查测试的密码是否正确:
There are three named groups in the regex. 1) "Numbers": will match a single number in the string. 2) "Alpha": will match a single character from "a" to "z" or "A" to "Z" 3) "Special": will match a single character not being "Alpha" or "Numbers"
正则表达式中有三个命名组。1) "Numbers": 将匹配字符串中的单个数字。2) "Alpha": 匹配从 "a" 到 "z" 或 "A" 到 "Z" 的单个字符 3) "Special": 匹配不是 "Alpha" 或 "Numbers" 的单个字符
Those three named groups are grouped in an alternative group, and {6,50}
advises regex machine to capture at least 6 of those groups mentiond above, but not more than 50.
这三个命名组被分组在一个备用组中,并{6,50}
建议正则表达式机器捕获上述组中的至少 6 个,但不超过 50 个。
To ensure a correct password is entered you have to check if there is a match, and after that, if the matched groups are capture as much as you desired. I'm a C# developer and don't know, how it works in javascript, but in C# you would have to check:
为确保输入正确的密码,您必须检查是否匹配,然后检查匹配的组是否按您的需要捕获。我是一名 C# 开发人员,不知道它在 javascript 中是如何工作的,但在 C# 中,您必须检查:
match.Groups["Numbers"].Captures.Count > 1
Hopefully it works the same in javascript! Good luck!
希望它在 javascript 中也能正常工作!祝你好运!
回答by chii
I use this
我用这个
export const validatePassword = password => {
const re = /^(?=.*[A-Za-z])(?=.*\d)[a-zA-Z0-9!@#$%^&*()~¥=_+}{":;'?/>.<,`\-\|\[\]]{6,50}$/
return re.test(password)
}
回答by Surya R Praveen
DEMO https://jsfiddle.net/ssuryar/bjuhkt09/
演示https://jsfiddle.net/ssuryar/bjuhkt09/
Onkeypress the function triggerred.
Onkeypress 功能触发。
HTML
HTML
<form>
<input type="text" name="testpwd" id="testpwd" class="form=control" onkeyup="checksPassword(this.value)"/>
<input type="submit" value="Submit" /><br />
<span class="error_message spassword_error" style="display: none;">Enter minimum 8 chars with atleast 1 number, lower, upper & special(@#$%&!-_&) char.</span>
</form>
Script
脚本
function checksPassword(password){
var pattern = /^.*(?=.{8,20})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%&!-_]).*$/;
if(!pattern.test(password)) {
$(".spassword_error").show();
}else
{
$(".spassword_error").hide();
}
}