在输入密码时,将密码的长度设置为6-16,且在密码长度出现小于6或是大于16时,会出现提示,效果如下图
1. 此事件为失去焦点事件,当鼠标离开密码框且点击旁边就会触发事件
2. 输入密码会出现提示,事件发生与密码的长度有关
3. 根据长度再来判断该提示的消息与效果.
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <meta http-equiv= "x-ua-compatible" content= "ie=edge" > <title>密码框</title> <style type= "text/css" > div{ width: 600px; margin: 100px auto; } .massage{ background-image: url(gth.png); background-repeat:no-repeat; background-position: left center; color:black; display: inline-block; padding-left: 20px; } .right{ background-image: url(right.png); background-repeat: no-repeat; color: green; } .wrong{ background-repeat: no-repeat; background-image: url(wrong.png); color: red; } </style> </head> <body> <div class= "register" > <input type= "password" id= "pwd" > <p class= "massage" >请输入6-16位密码</p> </div> <script> var pwd=document.queryselector( '#pwd' ); var aa=document.queryselector( '.massage' ); pwd.onblur= function (){ if ( this .value.length>16 || this .value.length<6){ aa.innertext= '错误,请输入6-16为密码' ; aa.classname= 'massage wrong' ; } else { aa.classname= 'massage right' ; aa.innertext= '密码正确' ; } } </script> </body> </html> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_46535360/article/details/108471017