Vibhuti.patel
Vibhuti.patel

Reputation: 63

validate password length after user leave password field

I want to check if the password length is at least 8 characters or not, when the user leaves the password field or press tab key. How can i do this?

My code for password is shown below.

<input type="password" name="password" id="pass1" placeholder="password"/> 

Upvotes: 1

Views: 25077

Answers (4)

David Castro
David Castro

Reputation: 1975

Password validation can use several rules, I used a service but the code inside the function can be reusable:

_validatePassword = function (validateUserNameRules, Model)
    {
        //bolean parameter validateUserNameRules -> true/false

        //this method recive a model like this:
        //Model.userName -> string
        //Model.password -> string
        //Model.password2 -> String

        var validationResult = {
            ResultId: 1, //1 success
            Message: "Password is correct."
            };

        if (validateUserNameRules && Model.userName == "") {

            validationResult.ResultId = 2;
            validationResult.Message = "Error: User name cannot be blank.";
            return (validationResult);
        }

        var re = /^\w+$/;
        if (validateUserNameRules && !re.test(Model.userName)) {

            validationResult.ResultId = 2;
            validationResult.Message = "Error: Username must contain only letters, numbers and underscores.";
            return (validationResult);

        }

        if (Model.password != "" && Model.password == Model.password2) {
            if (Model.password.length < 6) {
                validationResult.ResultId = 2;
                validationResult.Message = "Error: Password must contain at least six characters.";
                return (validationResult);
            }
            if (validateUserNameRules && Model.password == Model.userName) {
                validationResult.ResultId = 2;
                validationResult.Message = "Error: Password must be different from the Account Name.";
                return (validationResult);
            }
            re = /[0-9]/;
            if (!re.test(Model.password)) {
                validationResult.ResultId = 2;
                validationResult.Message = "Error: password must contain at least one number (0-9).";
                return (validationResult);
            }
            re = /[a-z]/;
            if (!re.test(Model.password)) {

                validationResult.ResultId = 2;
                validationResult.Message = "Error: password must contain at least one lowercase letter (a-z).";
                return (validationResult);

            }
            re = /[A-Z]/;
            if (!re.test(Model.password)) {

                validationResult.ResultId = 2;
                validationResult.Message = "Error: password must contain at least one uppercase letter (A-Z).";
                return (validationResult);
            }
        } else {
            validationResult.ResultId = 2;
            validationResult.Message = "Error: Please check that you've entered and confirmed your password.";
            return (validationResult);
        }

        return (validationResult); //success password validation!!
    };

Upvotes: 0

sanjay bhansali
sanjay bhansali

Reputation: 327

You can use javascript onchange event as below

and script code callfunction() as

function callfunction()
{


     var textBox = document.getElementById("pass1");
       var textLength = textBox.value.length;

         if(textBox.value=='' || textLength<=8)
         {
          alert('Please enter correct password');
         }


}

Upvotes: 2

Jai
Jai

Reputation: 74738

Use the jquery blur method for this.

$('#pass1').on('blur', function(){
    if(this.value.length < 8){ // checks the password value length
       alert('You have entered less than 8 characters for password');
       $(this).focus(); // focuses the current field.
       return false; // stops the execution.
    }
});

Fiddle for Demo

Upvotes: 8

Rickert
Rickert

Reputation: 1675

try this:

$('#pass1').on('blur', function(){
    if($(this).val().length > 8){
      alert('safe!');
    }
});

here is an example: http://jsfiddle.net/ACK2f/

Upvotes: 1

Related Questions