Javascript 密码验证的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2370015/
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
Regular Expression for password validation
提问by Andromeda
can any one help me in creating a regular expression for password validation.
任何人都可以帮助我创建用于密码验证的正则表达式。
The Condition is "Password must contain 8 characters and at least one number, one letter and one unique character such as !#$%&? "
条件是“密码必须包含 8 个字符和至少一个数字、一个字母和一个唯一字符,例如 !#$%&? "
回答by Macmade
^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$
---
^.* : Start
(?=.{8,}) : Length
(?=.*[a-zA-Z]) : Letters
(?=.*\d) : Digits
(?=.*[!#$%&? "]) : Special characters
.*$ : End
回答by Rohit Dubey
Try this
尝试这个
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})
Description of above Regular Expression:
上述正则表达式说明:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{8,20} # length at least 8 characters and maximum of 20
) # End of group
"/W" will increase the range of characters that can be used for password and pit can be more safe.
“/W”会增加可用于密码的字符范围,坑会更安全。
回答by Richard
You can achieve each of the individual requirements easily enough (e.g. minimum 8 characters: .{8,}will match 8 or more characters).
您可以很容易地满足每个单独的要求(例如,最少 8 个字符:.{8,}将匹配 8 个或更多字符)。
To combine them you can use "positive lookahead" to apply multiple sub-expressions to the same content. Something like (?=.*\d.*).{8,}to match one (or more) digits with lookahead, and 8 or more characters.
要组合它们,您可以使用“正向前瞻”将多个子表达式应用于相同的内容。类似于(?=.*\d.*).{8,}将一个(或多个)数字与前瞻匹配,以及 8 个或更多字符。
So:
所以:
(?=.*\d.*)(?=.*[a-zA-Z].*)(?=.*[!#$%&\?].*).{8,}
Remembering to escape meta-characters.
记住要转义元字符。
回答by Nasif Md. Tanjim
Password with the following conditions:
具有以下条件的密码:
- At least 1 digit
- At least 2 special characters
- At least 1 alphabetic character
No blank space
'use strict'; (function() { var foo = '3g^g$'; console.log(/^(?=.*\d)(?=(.*\W){2})(?=.*[a-zA-Z])(?!.*\s).{1,15}$/.test(foo)); /** * (?=.*\d) should contain at least 1 digit * (?=(.*\W){2}) should contain at least 2 special characters * (?=.*[a-zA-Z]) should contain at least 1 alphabetic character * (?!.*\s) should not contain any blank space */ })();
- 至少 1 位数字
- 至少 2 个特殊字符
- 至少 1 个字母字符
没有空格
'use strict'; (function() { var foo = '3g^g$'; console.log(/^(?=.*\d)(?=(.*\W){2})(?=.*[a-zA-Z])(?!.*\s).{1,15}$/.test(foo)); /** * (?=.*\d) should contain at least 1 digit * (?=(.*\W){2}) should contain at least 2 special characters * (?=.*[a-zA-Z]) should contain at least 1 alphabetic character * (?!.*\s) should not contain any blank space */ })();
回答by Ujjaval
You can make your own regular expression for javascriptvalidations;
您可以制作自己的正则表达式进行javascript验证;
(/^
(?=.*\d) //should contain at least one digit
(?=.*[a-z]) //should contain at least one lower case
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{8,} //should contain at least 8 from the mentioned characters
$/)
Example:- /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/
例子:- /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/

