任何人都可以帮助我创建用于密码验证的正则表达式。
条件是“密码必须包含 8 个字符和至少一个数字、一个字母和一个唯一字符,例如 !#$%&? "
任何人都可以帮助我创建用于密码验证的正则表达式。
条件是“密码必须包含 8 个字符和至少一个数字、一个字母和一个唯一字符,例如 !#$%&? "
^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$
---
^.* : Start
(?=.{8,}) : Length
(?=.*[a-zA-Z]) : Letters
(?=.*\d) : Digits
(?=.*[!#$%&? "]) : Special characters
.*$ : End
试试这个
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})
上述正则表达式说明:
( # 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”会增加可用于密码的字符范围,坑会更安全。
您可以很容易地满足每个单独的要求(例如,最少 8 个字符:.{8,}
将匹配 8 个或更多字符)。
要组合它们,您可以使用“正向前瞻”将多个子表达式应用于相同的内容。类似于(?=.*\d.*).{8,}
将一个(或多个)数字与前瞻匹配,以及 8 个或更多字符。
所以:
(?=.*\d.*)(?=.*[a-zA-Z].*)(?=.*[!#\$%&\?].*).{8,}
记住要转义元字符。
具有以下条件的密码:
没有空格
'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
*/
})();
您可以制作自己的正则表达式进行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
$/)
例子:- /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/