Aug
27th

Check Password Strength with JavaScript and Regular Expressions

Happy New Year! Marketing Technology finds and reports on the latest technology that will enable your business to effectively market to your audience, for acquisition or retention strategies. Subscribe now the Marketing Technology Blog RSS feed or to the Marketing Technology Email to have new content sent directly to your inbox. You'll also find my other business blog helpful, Social Media Domination.

Tonight I was doing some research on finding a good example of a Password Strength checker that uses JavaScript and Regular Expressions. In the application at my work, we do a post back to verify the password strength and it’s quite inconvenient for our users.

I found one example of some great Regular Expressions that look for a combination of length, characters and symbols, but the code was a little excessive for my taste and tailored for .NET. So I simplified the code and here’s a demonstration (if you’d like the code, simply right-click the link and save as):

Here’s the code. The Regular Expressions do a fantastic job of minimizing the length of the code:

<script language="javascript">
function passwordChanged() {
var strength = document.getElementById(’strength’);
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
var pwd = document.getElementById("password");
if (pwd.value.length==0) {
strength.innerHTML = ‘Type Password’;
} else if (false == enoughRegex.test(pwd.value)) {
strength.innerHTML = ‘More Characters’;
} else if (strongRegex.test(pwd.value)) {
strength.innerHTML = ‘<span style="color:green">Strong!</span>’;
} else if (mediumRegex.test(pwd.value)) {
strength.innerHTML = ‘<span style="color:orange">Medium!</span>’;
} else {
strength.innerHTML = ‘<span style="color:red">Weak!</span>’;
}
}
</script>
<input name="password" id="password" type="text" size="15" maxlength="20" onkeyup="return passwordChanged();" />
<span id="strength">Type Password</span>

Thanks to Andrew Cain for getting me started!

RSS feed | Trackback URI

15 Comments »

Comment by Jetman
2007-08-30 14:32:42

I found another password strength checkers. Their algorithm based on words dictionary. Try one at microsoft.com - http://www.microsoft.com/protect/yourself/password/checker.mspx and one at itsimpl.com - http://www.itsimpl.com
 
2007-09-18 19:35:36

[...] Check Password Strength with JavaScript and Regular Expressions | The Marketing Technology Blog - A little JavaScript goes a long way. [...]
 
Comment by Janis
2007-09-20 12:34:05

THANK YOU! THANK YOU! THANK YOU! I’ve been fooling around for 2 weeks with damn password strength code from other websites and pulling my hair out. Yours is short, works just like I want and best of all, easy for a javascript novice to modify! I wanted to capture the strength verdict and not let the form post to actually update the user’s password unless it met the strength test. Other people’s code was too complicated or didn’t work right or something else. I love you! XXXXX
Comment by Douglas Karr
2007-09-20 18:21:36

You’re welcome! You’re welcome! You’re welcome!

I love you, too!

 
 
Comment by rhodan
2007-10-01 18:18:19

thank god for people who can actually write a piece of code properly.
Had same experience as Janis.

This works right out of the box which is perfect for people like me who cant code javascript!

 
Comment by SiteOne
2007-10-11 04:11:29

Thank you for writing a piece of code that does exactly what it says on the can!
 
Comment by Nisreen
2007-10-16 02:11:00

Hi,first of all thanks alot for ur efforts,I tried to use this with Asp.net but didn’t work,i’m using

instead of tag,and it didn’t work,any suggestions?!

 
Comment by Jim
2007-10-24 10:19:50

To Nisreen: the code in the highlighted box doesn’t work with a cut’n'paste. The single quote is messed up. The demonstration link’s code is fine though.
 
2007-10-28 17:16:34

[...] and you will get an offer that’s only available to folks that read my feed!A while ago I put up a Password Strength Checker using JavaScript and Regular Expressions. On that same note, you can also check the structure of an email address utilizing the same [...]
 
2007-10-28 17:18:44

[...] Password Strength - If you require a certain password strength (combination of alphanumeric characters or cases), then provide some feedback for me while I’m typing my password in. Don’t wait for me to submit before telling me it failed. [...]
 
Comment by Jaap
2008-04-13 15:04:47

Hey, I like your script! I translated it into dutch, and I posted it at my forum here!
 
Comment by mont
2008-06-13 00:26:56

great job! exactly how it should be done on the client
 
Comment by micky
2008-07-17 02:45:54

very nice job….

Thanks Douglas, I use it for my current job.

 
2008-08-11 10:51:42

[...] server validation, but errored on IE which does client-side validation. I googled a bit and found a similar expression by Douglas Karr  var strongRegex = new [...]
 
Comment by Pierre
2008-09-03 09:55:34

“P@s$w0rD” shows at strong, although it would be cracked fairly quickly with a dictionnary attack…
To deploy such a feature on a professionnal solution, I believe it is important to combine this algorithm with a dictionnary check.
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

My Comment Policy: I moderate comments. Please be patient:

  • Spam will happily be destroyed.
  • Use your real name, not some keywords. Otherwise it will be destroyed.
  • Mean comments aren't necessary. If I don't post them I will reply personally to let you know why.
  • Lewd comments will be edited, I don't want my readers leaving because of offensive content.
Great debate, criticism and colorful commentary is always appreciated and approved!