Check Password Strength with JavaScript and Regular Expressions | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Check Password Strength with JavaScript and Regular Expressions

Thursday

Javascript Password Strength Meter:
This post is  good example of a Password Strength checker that uses JavaScript and Regular Expressions. It check the following conditions to create secure password.
  • The password must contains greater than 6 character
  • The password must have both lower and uppercase characters
  • The password must have at least one number
  • The password must have at least one special character
   
PasswordStrength.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  
    <title>Dotnetcluster-Password strength checker</title>
<!-- http://dotnetcluster.blogspot.in -->
    <script language="javascript" type="text/javascript">
 
function pwdStrength(password)
{
        var desc = new Array();
        desc[0] = "Very Weak";
        desc[1] = "Weak";
        desc[2] = "Better";
        desc[3] = "Medium";
        desc[4] = "Strong";
        desc[5] = "Strongest";
        var score   = 0;
        //if password bigger than 6 give 1 point
        if (password.length > 6) score++;
        //if password has both lower and uppercase characters give 1 point      
        if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;
        //if password has at least one number give 1 point
        if (password.match(/\d+/)) score++;
        //if password has at least one special caracther give 1 point
        if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;
        //if password bigger than 12 give another 1 point
        if (password.length > 12) score++;
         document.getElementById("pwdDescription").innerHTML = desc[score];
         document.getElementById("pwdStrength").className = "strength" + score;
}
  
    </script>
</head>
<body>
    <table>
        <tr>
            <td>
          
                    Password<input type="password" name="pass" id="pass" onkeyup="pwdStrength(this.value)" />
            </td>
            <td>
                <div id="pwdDescription" style="color: red"">
                    <b>Password</b>
                </div>
            </td>
        </tr>
       
    </table>
</body>
</html>
 Demo for check password strength:

Password
Password
That's all. If this post is useful for you , please share this to your friends. Thanks!

2 comments:

Unknown said...

Thanks :)

Unknown said...

It really worked well

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets