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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22323414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:55:19  来源:igfitidea点击:

validate password length after user leave password field

javascriptjquerypasswords

提问by Vibhs

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?

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

My code for password is shown below.

我的密码代码如下所示。

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

回答by Jai

Use the jquery blurmethod for this.

blur为此使用 jquery方法。

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

演示小提琴

回答by Rickert

try this:

试试这个:

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

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

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

回答by sanjay bhansali

You can use javascript onchange event as below

您可以使用 javascript onchange 事件如下

and script code callfunction() as

和脚本代码 callfunction() 为

function callfunction()
{


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

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


}

回答by David Castro

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