Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx question for password strength validation

I'm looking for a single regular expression for our password requirements. Passwords:

  • Must be at least 8 characters
  • Cannot contain spaces
  • Contain both lowercase and UPPERCASE characters
  • Contain at least one numeric digit
  • Contain at least one special character (i.e. any character not 0-9,a-z,A-Z)
like image 949
user731361 Avatar asked Apr 29 '11 15:04

user731361


2 Answers

It'll probably be easier to code the logic. Regex is used for matching patterns. Passwords tend to be somewhat random strings, so the problem doesn't lend itself easily to be solved by a regex. It's possible but will be cryptic to read and hard to maintain.

like image 176
armandino Avatar answered Oct 22 '22 04:10

armandino


Idea and most of the work taken from http://www.zorched.net/2009/05/08/password-strength-validation-with-regular-expressions/

^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$

I used the basic answer at the bottom of his post, but replaced all the dots with \S to rule out space characters, and moved around some of the assertions.

like image 44
Brigham Avatar answered Oct 22 '22 02:10

Brigham