Forum Moderators: open

Message Too Old, No Replies

RegExp to check for "strong password"

         

Fotiman

1:54 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I need to check a string to verify that it contains at least 3 of these characters sets:
- Upper case letters
- Lower case letters
- Digits
- Special characters (!, @, $, %, etc.)

Is there a way to write a RegExp that will perform this check?

Thanks.

jshanman

2:18 pm on Jun 23, 2006 (gmt 0)

10+ Year Member



You could write 4 reg exp and count the matches...

var ok = 0;
//- Upper case letters
if (passValue.match(/[A-Z]/)) ok++;

//- Lower case letters
if (passValue.match(/[a-z]/)) ok++;

//- Digits
if (passValue.match(/[0-9]/)) ok++;

//- Special characters (!, @, $, %, etc.)
if (passValue.match(/[@#$%&!*)(-+=^]/)) ok ++;

if (ok > 2) {
alert('good password!');
} else {
alert('pick a stronger password');
}

- JS

Fotiman

2:27 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Very nice! Thanks so much!

eelixduppy

9:08 pm on Jun 26, 2006 (gmt 0)



It's generally not a good idea to validate forms with javascript because it can easily be avoided and data that you don't necessarily want may go into your database, etc... A much more secure method would be to do it server-side.

coopster

10:45 pm on Jun 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



True, and very good advice to check server-side. The advantage to doing both is that the user doesn't have to make a round trip to the server before receiving error messages.