用户离开密码字段后验证密码长度

时间:2014-03-11 10:48:49

标签: javascript jquery passwords

当用户离开密码字段或按Tab键时,我想检查密码长度是否至少为8个字符。 我怎么能这样做?

我的密码代码如下所示。

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

4 个答案:

答案 0 :(得分:5)

使用jquery blur方法。

$('#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

答案 1 :(得分:1)

试试这个:

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

这是一个例子:http://jsfiddle.net/ACK2f/

答案 2 :(得分:1)

您可以使用javascript onchange事件,如下所示

和脚本代码callfunction()为

function callfunction()
{


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

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


}

答案 3 :(得分:0)

密码验证可以使用多个规则,我使用了服务,但函数内部的代码可以重复使用:

_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!!
    };