Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
905 views
in Technique[技术] by (71.8m points)

regex - Regular expression for password (at least 2 digits and one special character and minimum length 8)

I have been searching for regular expression which accepts at least two digits and one special character and minimum password length is 8. So far I have done the following: [0-9a-zA-Z!@#$%0-9]*[!@#$%0-9]+[0-9a-zA-Z!@#$%0-9]*

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Something like this should do the trick.

^(?=(.*d){2})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z!@#$%]{8,}

(?=(.*d){2}) - uses lookahead (?=) and says the password must contain at least 2 digits

(?=.*[a-zA-Z]) - uses lookahead and says the password must contain an alpha

(?=.*[!@#$%]) - uses lookahead and says the password must contain 1 or more special characters which are defined

[0-9a-zA-Z!@#$%] - dictates the allowed characters

{8,} - says the password must be at least 8 characters long

It might need a little tweaking e.g. specifying exactly which special characters you need but it should do the trick.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...