Regular Expression to Check Password Strength
Submitted by vaibhav on Mon, 11/02/2009 - 01:57
Last few days I was trying hard to come up with a Regular Expression for checking password strength in JavaScript. For those who don't know what is a regular expression and How it can be used, Here is a wonderful link for you. Regular Expression can be a very handy code, for Matching two strings, Data Validation and even Syntax Highlighting.
I could have use Ajax toolkit for checking password strength in a Asp.Net page. But that would be done at Server Side. So I decided to make it in JavaScript. I wrote this little but efficient script to check password strength at Client Side.
<script type="text/javascript"> function passwordcheck() { var password=document.getElementById('txtPassword').value; var noofchar=/^.*(?=.{6,}).*$/; var checkspace=/\s/; var best=/^.*(?=.{6,})(?=.*[A-Z])(?=.*[\d])(?=.*[\W]).*$/; var strong=/^[a-zA-Z\d\W_]*(?=[a-zA-Z\d\W_]{6,})(((?=[a-zA-Z\d\W_]*[A-Z])(?=[a-zA-Z\d\W_]*[\d]))|((?=[a-zA-Z\d\W_]*[A-Z])(?=[a-zA-Z\d\W_]*[\W_]))|((?=[a-zA-Z\d\W_]*[\d])(?=[a-zA-Z\d\W_]*[\W_])))[a-zA-Z\d\W_]*$/; var weak=/^[a-zA-Z\d\W_]*(?=[a-zA-Z\d\W_]{6,})(?=[a-zA-Z\d\W_]*[A-Z]|[a-zA-Z\d\W_]*[\d]|[a-zA-Z\d\W_]*[\W_])[a-zA-Z\d\W_]*$/; var bad=/^((^[a-z]{6,}$)|(^[A-Z]{6,}$)|(^[\d]{6,}$)|(^[\W_]{6,}$))$/; if (true==checkspace.test(password)) tdPwdStrength.innerHTML="spaces are not allowed"; else if (false==noofchar.test(password)) { tdWeak.bgColor="transparent"; tdStrong.bgColor="transparent"; tdBest.bgColor="transparent"; tdBad.bgColor="transparent" tdPwdStrength.innerHTML="must be 6 char"; } else if(best.test(password)) { tdBad.bgColor="green"; tdWeak.bgColor="green"; tdStrong.bgColor="green"; tdBest.bgColor="green"; tdPwdStrength.innerHTML="best"; } else if(strong.test(password)) { tdBad.bgColor="yellow"; tdWeak.bgColor="yellow"; tdStrong.bgColor="yellow"; tdBest.bgColor="transparent" tdPwdStrength.innerHTML="Strong"; } else if(weak.test(password)==true && bad.test(password)==false) { tdBad.bgColor="orange"; tdWeak.bgColor="orange"; tdStrong.bgColor="transparent"; tdBest.bgColor="transparent"; tdPwdStrength.innerHTML="weak"; } else if(bad.test(password)) { tdWeak.bgColor="transparent"; tdStrong.bgColor="transparent"; tdBest.bgColor="transparent"; tdBad.bgColor="red"; tdPwdStrength.innerHTML="Bad"; } } </script> <html> <input type="text" id="txtPassword" onkeyup="passwordcheck()"/> <span id="tdPwdStrength">Password Strength</span> <table width="100px" > <tr style="height:25px"> <td id="tdBad" ></td> <td id="tdWeak"></td> <td id="tdStrong"></td> <td id="tdBest"></td> </tr> </table> </html>
Thanks for the script. I am
Thanks for the script. I am going to use it on my website. Can you please tell me how to check qwerty, asdf,pqrs type strings in passwords.
good guys
good guys
goodgoogd
goodgoogd
Thanks
Thanks a lot... This is greate!!!
Post new comment