javascript解鎖前端密碼框常見功能做法
更新時間:2022年03月14日 13:11:20 作者:館主阿牛
這篇文章主要為大家介紹了javascript解鎖前端密碼框常見功能做法示例演示,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
前言
學前端最基本的登錄頁面肯定要會寫,那登錄頁面里面的密碼框的功能設(shè)計就需要好好打磨,主要功能有顯示密碼明文,密碼檢測信息提示等等,那么本篇博客將寫寫這些功能結(jié)合js怎么做,很簡單,看一下就會了。
顯示隱藏密碼明文
1.用到的圖片素材


2.代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
position: relative;
width: 400px;
border-bottom: 1px solid #ccc;
margin: 100px auto;
}
.box input{
width: 370px;
height: 30px;
border: 0;
outline: none;
}
.box .image{
position: absolute;
top:2px;
right: 2px;
border: 1px solid #ccc;
background-color: #ccccccba;
border-radius: 50%;
overflow: hidden;
}
.box img{
vertical-align: bottom; /*父盒子是由img撐開的,所以要去除下面的間隙*/
width: 22px;
}
</style>
</head>
<body>
<div class="box">
<div class="image">
<img src="close.png" alt="" id="img">
</div>
<input type="password" id="pwd">
</div>
<script>
var img = document.getElementById('img');
var pwd = document.getElementById('pwd');
var flag = 0;
img.onclick = function(){
if(flag == 0){
pwd.type = 'text';
this.src='open.png'; // this指向事件函數(shù)的調(diào)用者,即img
flag = 1;
}else{
pwd.type = 'password';
this.src='close.png';
flag = 0;
}
}
</script>
</body>
</html>
3.結(jié)果演示

密碼框驗證信息
1.用到的圖片素材



2.代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.register{
width: 600px;
margin: 100px auto;
}
.ipt{
width: 250px;
border: 1px solid #999;
outline: none;
}
.message{
display: inline-block;
height: 20px;
font-size: 12px;
color: #999;
background: url(mess.png) no-repeat left center;
padding-left: 30px;
}
.wrong{
color: red;
background: url(wrong.png) no-repeat left center;
}
.right{
color: green;
background: url(right.png) no-repeat left center;
}
</style>
</head>
<body>
<div class="register">
<input type="password" class="ipt">
<p class="message">請輸入6~16位密碼</p>
</div>
<script>
var ipt = document.querySelector('.ipt');
var msg = document.querySelector('.message');
ipt.onfocus = function(){
this.style.borderColor = 'skyblue'
}
ipt.onblur = function(){
this.style.borderColor = '#999';
if(this.value.length<6 || this.value.length>16){
msg.className = 'message wrong';
msg.innerHTML = '你輸入的位數(shù)不符合要求6~16位!';
}else{
msg.className = 'message right';
msg.innerHTML = '你輸入的正確!';
}
}
</script>
</body>
</html>
3.結(jié)果演示

結(jié)語
以上就是javascript解鎖前端密碼框常見功能做法的詳細內(nèi)容,更多關(guān)于javascript前端密碼框功能做法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
元素未顯示設(shè)置width/height時IE中使用currentStyle獲取為auto
元素未顯示設(shè)置width/height時IE中無法使用currentStyle獲取,默認獲取值為auto,需要的朋友可以參考下2014-05-05
Javascript實現(xiàn)禁止輸入中文或英文的例子
這篇文章主要介紹了Javascript實現(xiàn)禁止輸入中文或英文的方法實例,本文方法都是使用正則表達式實現(xiàn),需要的朋友可以參考下2014-12-12

