Click here to Skip to main content
15,921,622 members
Articles / Web Development / HTML
Tip/Trick

Strong Password Validation

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
17 May 2012CPOL1 min read 42.3K   465   4   8
Regular Expression for a strong password using jQquery AJAX.

Introduction

Today I am going to demonstrate a validation trick for a strong password. We often need a strong password as it is became a standard of web to have Passwords very strong so that nobody can guess it easily. It will be very useful for any registration form.

Background 

In this tip I used jQuery.ajax method to call the Validation Method on server side. The idea behind this is to avoid post back and also it is very light and quick. People who are new to jquery please go through this link.

In this example on the login page there are two input fields User Name and password, our focus in on the Password  filed. The validation trick will look for the input by user. Password must use a combination of these:

  • Atleast 1 upper case letters (A – Z)
  • Lower case letters (a – z)
  • Atleast 1 number (0 – 9)
  • Atleast 1 non-alphanumeric symbol (e.g. @ ‘$%£!’)

Using the code

When you are using jQuery.ajax then on the server side the there must be a WebMethod. Using jQuery I captured the Click event of the Register button and call the WebMethod using jQuery.ajax by passing the Password field value as perameter:

JavaScript
<script type="text/javascript">
    $(document).ready(function () {
        //on Button click event
        $('#btnSubmit').click(function () {
            validate($('#txtPassword').val()); 
        });
    });
    var error = "Password must use a combination " + 
      "of these:<br />I.Atleast 1 upper case letters (A – Z)" + 
      "<br />II.Lower case letters (a – z)<br />III." + 
      "Atleast 1 number (0 – 9)<br />IV.Atleast 1 " + 
      "non-alphanumeric symbol (e.g. @ ‘$%£!’)";

    function validate(val) {
        $('#TDStatus').html('');
        $.ajax({
            type: "POST",
            url: "Login.aspx/ValidatePassword",
                data: "{'password':'" + val + "'}",
                contentType: "application/json; 

            charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if (msg.d.length > 0) {
                        if (msg.d == "success") {
                            $('#TDStatus').html('Congratulations! Your password is strong.');
                        }
                        else if (msg.d == "fail") 
                        { 
                            $('#TDStatus').html(error);
                        }
                    }
                },
                async: false,
                error: function (xhr, status, error) { 
                    //alert(xhr.statusText);
                    $('#TDStatus').html('<center>Error occured!</center>');
                }
            });
        }
</script>

On the serverside there is a WebMethod that will check the password field for the required validations. If it passes all four constraints then it will return success other wise it will return fail.

C#
[WebMethod]
public static string ValidatePassword(string password)
{
    string strResult = "fail";
    try
    {
        bool result = false;
        bool isDigit = false;
        bool isLetter = false;
        bool isLowerChar = false;
        bool isUpperChar = false;
        bool isNonAlpha = false;

        foreach (char c in password)
        {
            if (char.IsDigit(c))
                isDigit = true;
            if (char.IsLetter(c))
            {
                isLetter = true;
                if (char.IsLower(c))
                    isLowerChar = true;
                if (char.IsUpper(c))
                    isUpperChar = true;
            }
            Match m = Regex.Match(c.ToString(), @"\W|_");
            if (m.Success)
                isNonAlpha = true;
        }

        if (isDigit && isLetter && isLowerChar && 
                               isUpperChar && isNonAlpha)
            result = true;

        if (result)
            strResult = "success";
    }
    catch
    {
        strResult = "fail";
    }
    return strResult;
}

Screenshot

Image 1

History

  • First release on 17 May, 2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer LIFELONG Pakistan, Islamabad
Pakistan Pakistan
Software Engineer at LIFELONG Pakistan,

http://tanweerbravi.blogspot.com

Comments and Discussions

 
GeneralMy vote of 4 Pin
♥…ЯҠ…♥29-May-12 23:07
professional♥…ЯҠ…♥29-May-12 23:07 
GeneralRe: My vote of 4 Pin
tanweer29-May-12 23:15
tanweer29-May-12 23:15 
GeneralMy vote of 4 Pin
thalavi19-May-12 19:39
thalavi19-May-12 19:39 
QuestionServer validation? Pin
JoseMenendez17-May-12 5:43
JoseMenendez17-May-12 5:43 
AnswerRe: Server validation? Pin
Marc Bernier17-May-12 7:14
Marc Bernier17-May-12 7:14 
GeneralRe: Server validation? Pin
JoseMenendez17-May-12 7:18
JoseMenendez17-May-12 7:18 
With all due respect, your question/suggestion is even stupider. The example here needs javascript to make the validation using Ajax.

Of course server side validation is going to be necessary when the password is submitted. Mi point is, the author of the article is using a full call to the server so it can check character patterns.
J

GeneralRe: Server validation? Pin
tanweer17-May-12 18:48
tanweer17-May-12 18:48 
AnswerRe: Server validation? Pin
tanweer17-May-12 18:49
tanweer17-May-12 18:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.