JS 实现密码显隐切换

简介: JS 实现密码显隐切换

今天我们要写的是一个常用的小案例,密码隐藏与显示

样式如下:


input 标签的密码框自带有隐藏密码的功能,这篇文章就当做隐藏密码的内部实现机制吧......(而且我不知道如何去除 input 自带的隐藏密码图标,有会的大佬留个言呗!!)


<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>body{
background-color: rgb(204, 204, 204);
       }
#divout{box-sizing: border-box;
position: absolute;
top: 50%;
left: 50%;
margin-left:-216px;
margin-top: -160px;
display: inline-block;
width: 450px;
height: 180px; 
background-color: rgb(204, 204, 204);
  }
#divout #div1{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 55px;
background-color: rgb(255, 255, 255);
border-bottom:3pxsolid;
border-color: rgb(109, 109, 109) ;
background-color: rgb(204, 204, 204);
padding-left: 10px;
padding-top: 5px;
  }
#divout #div2{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 70px;
background-color: rgb(204, 204, 204);
  }
#divout #div3{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 55px;
background-color: rgb(255, 255, 255);
border-bottom:3pxsolid;
border-color: rgb(109, 109, 109) ;
background-color: rgb(204, 204, 204);
padding-left: 10px;
padding-top: 5px;
  }
#text1{height: 44px;
width: 360px;
border: 0;
outline: none;
font-size: 24px;
background-color: rgb(204, 204, 204);
color:   rgb(109, 109, 109) ;
  }
#pwd{height: 44px;
width: 360px;
border: 0;
outline: none;
font-size: 24px;
background-color: rgb(204, 204, 204);
color:   rgb(109, 109, 109) ;
  }
img{
position:absolute;
right: 19px;
top: 145px;
width: 30px;
height: 30px;
  }
</style></head><body><divid="divout"><divid="div1"><inputtype="text"id="text1"placeholder="请输入用户名"></div><divid="div2"></div><divid="div3"><label><imgsrc="./登录/密码隐藏 (2).png"alt=""id="img1"></label><inputtype="password"id="pwd"autocomplete="off"placeholder="请输入密码"></div></div><script>varpwd=document.getElementById('pwd');
varimg=document.getElementById('img1');
varflag=0;
img.onclick=function(){
if(flag==0){
pwd.type="text";
img.src="./登录/密码显示.png";
flag=1;
            }else{
pwd.type="password";
img.src="./登录/密码隐藏 (2).png";
flag=0;
            }
        }
</script></body></html>改进代码:增加了密码输入对错提示<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>body{
background-color: rgb(204, 204, 204);
       }
#divout{box-sizing: border-box;
position: absolute;
top: 50%;
left: 50%;
margin-left:-216px;
margin-top: -160px;
display: inline-block;
width: 450px;
height: 180px; 
background-color: rgb(204, 204, 204);
  }
#divout #div1{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 55px;
background-color: rgb(255, 255, 255);
border-bottom:3pxsolid;
border-color: rgb(109, 109, 109) ;
background-color: rgb(204, 204, 204);
padding-left: 10px;
padding-top: 5px;
  }
#divout #div2{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 70px;
background-color: rgb(204, 204, 204);
  }
#divout #div3{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 55px;
background-color: rgb(255, 255, 255);
border-bottom:3pxsolid;
border-color: rgb(109, 109, 109) ;
background-color: rgb(204, 204, 204);
padding-left: 10px;
padding-top: 5px;
  }
#text1{height: 44px;
width: 360px;
border: 0;
outline: none;
font-size: 24px;
background-color: rgb(204, 204, 204);
color:   rgb(109, 109, 109) ;
  }
#pwd{height: 44px;
width: 360px;
border: 0;
outline: none;
font-size: 24px;
background-color: rgb(204, 204, 204);
color:   rgb(109, 109, 109) ;
  }
img{
position:absolute;
right: 19px;
top: 145px;
width: 30px;
height: 30px;
  }
#yn{box-sizing: border-box;
position:absolute;
right: 50px;
top: 400px;
width: 400px;
height: 60px;
  }
  .message{
background:url(./登录/正确.png)  no-repeatleftcenter ;
background-size: 5%100%;
padding-left: 25px;
font-weight: bold;
color: rgb(68, 68, 68);
display: none;
  }
  .wrong{
background: url(./登录/错误空心.png) no-repeatleftcenter;
background-size: 5%100%;
color: brown;
  }
  .right{
background: url(./登录/正确.png) no-repeatleftcenter;
background-size: 5%100%;
color: rgb(6, 189, 46);
  }
</style></head><body><divid="divout"><divid="div1"><inputtype="text"id="text1"placeholder="请输入用户名"></div><divid="div2"></div><divid="div3"><label><imgsrc="./登录/密码隐藏 (2).png"alt=""id="img1"></label><inputtype="password"id="pwd"autocomplete="off"placeholder="请输入密码"></div></div><divid="yn"><pclass="message">请输入正确的6-16位的密码</p></div><script>varpwd=document.getElementById('pwd');
varimg=document.getElementById('img1');
varmes=document.querySelector('.message');
pwd.onfocus=function(){
mes.style.display='block';
        }
pwd.onblur=function(){
if(pwd.value.length<6||pwd.value.length>16){
mes.className='message wrong';
mes.innerHTML='请输入正确的符合要求的密码';
            }else{
mes.className='message right';
mes.innerHTML='符合要求'            }
        }
varflag=0;
img.onclick=function(){
if(flag==0){
pwd.type="text";
img.src="./登录/密码显示.png";
flag=1;
            }else{
pwd.type="password";
img.src="./登录/密码隐藏 (2).png";
flag=0;
            }
        }
</script></body></html>
相关文章
|
5月前
|
JavaScript 算法 数据安全/隐私保护
原生JS实现:密码输入框显示隐藏密码效果
原生JS实现:密码输入框显示隐藏密码效果
141 4
|
5月前
|
JavaScript 数据安全/隐私保护
JS正则验证密码
JS正则验证密码
59 0
|
3天前
|
人工智能 JavaScript 网络安全
ToB项目身份认证AD集成(三完):利用ldap.js实现与windows AD对接实现用户搜索、认证、密码修改等功能 - 以及针对中文转义问题的补丁方法
本文详细介绍了如何使用 `ldapjs` 库在 Node.js 中实现与 Windows AD 的交互,包括用户搜索、身份验证、密码修改和重置等功能。通过创建 `LdapService` 类,提供了与 AD 服务器通信的完整解决方案,同时解决了中文字段在 LDAP 操作中被转义的问题。
|
6天前
|
前端开发 JavaScript 安全
前端JS实现密码校验键盘横竖、26字母、相同字母、相同数字、密码包含用户名、数字 字母不能连续 不能相同三个、不能横向 竖向 连续三个 包含字符、不能有中文符号
该 JavaScript 代码实现了一个严格的密码校验功能,确保密码满足多种安全要求,包括长度、字符类型、不包含中文及特殊字符、不与用户名相似等。通过多个辅助函数,如 `validateFormat` 检查密码格式,`isHasChinaCharFun` 检测中文符号,`getCharAll` 生成键盘组合,以及 `checkPasswordFun` 综合验证密码的有效性和安全性。此工具对于提高用户账户的安全性非常有用。
16 0
|
2月前
|
前端开发 JavaScript 数据安全/隐私保护
前端JS正则校验密码之3种实现方式
这篇文章展示了三种使用JavaScript正则表达式来校验密码的方法,密码需要满足包含大写字母、小写字母、数字及特殊字符,并在8到16位之间,同时提供了示例代码和实现效果。
72 1
前端JS正则校验密码之3种实现方式
|
2月前
|
JavaScript 前端开发 API
vue中将验证表单输入框的方法写在一个js文件中(表达式验证邮箱、身份证、号码、两次输入的密码是否一致)
这篇文章介绍了如何在Vue框架中将表单输入验证逻辑封装到一个JavaScript文件中,并通过正则表达式验证邮箱、身份证、手机号等信息,同时确保两次密码输入的一致性。
|
5月前
|
移动开发 JavaScript 安全
js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结
js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结
231 0
|
4月前
|
算法 JavaScript 安全
一篇文章讲明白JavaScript_提交表单和MD5算法密码加密
一篇文章讲明白JavaScript_提交表单和MD5算法密码加密
41 0
|
4月前
|
算法 JavaScript 安全
一篇文章讲明白JavaScript_提交表单和MD5算法密码加密
一篇文章讲明白JavaScript_提交表单和MD5算法密码加密
88 0
|
4月前
|
JavaScript 前端开发 数据安全/隐私保护
JS中使用Cookie实现记住密码以及设置密码过期时间
JS中使用Cookie实现记住密码以及设置密码过期时间
72 0