Regular Expression to Check Password Strength

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.

Take a Demonstration

<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> 

Anonymous's picture

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.

Anonymous's picture

good guys

good guys

Anonymous's picture

goodgoogd

goodgoogd

Anonymous's picture

Thanks

Thanks a lot... This is greate!!! 

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr><br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong><font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • You can enable syntax highlighting of source code with the following tags: <c>, <cpp>, <csharp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <sql>, <tsql>, <vbnet>, <xml>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Lines and paragraphs break automatically.

More information about formatting options