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

Vib*_*bhs 1 javascript passwords jquery

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

我的密码代码如下所示.

<input type="password" name="password" id="pass1" placeholder="password"/> 
Run Code Online (Sandbox Code Playgroud)

Jai*_*Jai 5

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.
    }
});
Run Code Online (Sandbox Code Playgroud)

小提琴演示