1

我想要一个检查“最少 8 个字符,至少一个数字和一个特殊字符,最多 32 个字符”的正则表达式。,我的正则表达式是:

^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{8,16}$
4

2 回答 2

4

I test this regex can work satisfied

/(?=^.{8,32}$)(?=(?:.*?\d){1})(?=.*[a-z])(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$/

you can watch the live demo : http://jsfiddle.net/tuxrM/

var re = new RegExp(/(?=^.{8,32}$)(?=(?:.*?\d){1})(?=.*[a-z])(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$/);

var test1 = '1234567',      //  less than 8 characters
    test2 = 'aaaaaaaa',     //  no  number & no special character
    test3 = 'aaaaaaa1',     //  no special character
    test4 = 'aaaAaa#1',     //  satisfied
    test5 = 'abcdefghigklmnopqrstuvwxyz1234567890332123';  //  more than 32 characters

re.test(test1);     //  FALSE
re.test(test2);     //  FALSE
re.test(test3);     //  FALSE
re.test(test4);     //  TRUE
re.test(test5);     //  FALSE
于 2013-07-09T09:33:16.370 回答
2

这实际上最好通过 3 个不同的正则表达式来解决。

/[0-9]/ //Check for at least one number.
/(SPECIAL CHARACTER)/ //Check for at least one special character. Please define "special" character.

接着

str.length >= 8 && str.length <= 32

如果这是密码,请不要限制您的用户密码长度。

于 2013-07-09T08:58:53.317 回答