Home  >  Article  >  Web Front-end  >  How to hide and display passwords in html

How to hide and display passwords in html

藏色散人
藏色散人Original
2021-06-03 11:17:3420533browse

htmlHow to realize password hiding and display: first write the HTML interface tag and css style; then directly modify "type=text" and "type=password" to display and hide the password input box.

How to hide and display passwords in html

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.

htmlHow to hide and display passwords?

Implementation of text display and hiding of password input box in HTML5 form

Problem description and ideas

The password type of password input box in HTML5 form can be Hide the content entered by the user, but sometimes it is used to allow the user to freely display or hide the content of the input box: to implement this function, the first thing that comes to mind is to use js to dynamically change the type of input, that is, type = password to type = text hidden The password will be displayed, but in fact it cannot be realized and has no effect, so I can only change my mind:

[Update: The latest type setting has taken effect, you can directly modify type=text and type=password To display and hide the password input box, the updated code is shown below. Please abandon the original method]

Put two inputs, one is password and the other is text, to jointly monitor the user's input event. Each time the user For this switch, we use js to control the display and hiding of the two inputs to achieve this effect.

How to hide and display passwords in html

Implementation steps:

First write the HTML interface tags and css style (the text input is hidden first: style ="display:none", subsequent display and hiding operations are also implemented by changing the display attribute)

CSS:

<style type="text/css">
body{
margin:0px;  
background-color: white; 
font-family: &#39;PT Sans&#39;, Helvetica, Arial, sans-serif;  
text-align: center;  
color: #A6A6A6;  
}
/*输入框样式,去掉背景阴影模仿原生应用的输入框*/
input{
width: 100%;  
height: 50px;  
border:none;  
padding-left:3px;  
font-size: 18px;  
}
input:focus {  
    outline: none;  
}
/*显示隐藏密码图片*/
img{
width: 40px;
height: 25px;
position: absolute;  
right: 0px; 
margin: 15px;  
}
/*登录按钮*/
button{  
    width: 200px;  
    height: 50px;  
    margin-top: 25px;  
    background: #1E90FF;  
    border-radius: 10px;  
    border:none;
    font-size: 18px;
    font-weight: 700;  
    color: #fff;
}
button:hover {
background: #79A84B;  
outline: 0;
}
/*输入框底部半透明横线*/
.input_block {
border-bottom: 1px solid rgba(0,0,0,.1);
}
/*container*/
#page_container{
margin: 50px;
}
</style>

HTML:

<div id="page_container">
<!--暗文密码输入框-->
<div class="input_block" id="psw_invisible">
<img  id="visible" οnclick="showPsw()" src="visible.png" alt="How to hide and display passwords in html" >
<input type="password" id="input_invisible" placeholder="Password"/>
</div>
<!--明文密码输入框-->
<div class="input_block" id="psw_visible" style="display: none;">
<img  id="invisible" οnclick="hidePsw()" src="invisible.png" alt="How to hide and display passwords in html" >
<input type="text" id="input_visible" placeholder="Password"/>
</div>
 
<button οnclick="">Login</button>
</div>

Then use JS to implement Alternate operation of click events: the password is hidden at first, click the small eye icon at the back to display the password, that is, the password input is hidden and the text input is displayed. At the same time, pay attention to passing the password value to the text, and vice versa. Here’s a lesson:

JS:

<script type="text/javascript">
// 这里使用最原始的js语法实现,可对应换成jquery语法进行逻辑控制
var visible=document.getElementById(&#39;psw_visible&#39;);//text block
var invisible=document.getElementById(&#39;psw_invisible&#39;);//password block
var inputVisible = document.getElementById(&#39;input_visible&#39;);
var inputInVisible = document.getElementById(&#39;input_invisible&#39;);
    //隐藏text block,显示password block
function showPsw(){
var val = inputInVisible.value;//将password的值传给text
inputVisible.value = val;
invisible.style.display = "none";  
visible.style.display = "";  
}
    //隐藏password,显示text  
    function hidePsw(){
   var val=inputVisible.value;//将text的值传给password  
inputInVisible.value = val; 
invisible.style.display = "";  
    visible.style.display = "none";  
}
</script>

The updated code is as follows:

HTML:

<div id="page_container">
<!--密码输入框-->
<div class="input_block">
<img  id="demo_img" οnclick="hideShowPsw()" src="visible.png" alt="How to hide and display passwords in html" >
<input type="password" id="demo_input" placeholder="Password"/>
</div>
 
<button οnclick="">Login</button>
</div>

JS:

<script type="text/javascript">
// 这里使用最原始的js语法实现,可对应换成jquery语法进行逻辑控制
var demoImg = document.getElementById("demo_img");
var demoInput = document.getElementById("demo_input");
    //隐藏text block,显示password block
function hideShowPsw(){
if (demoInput.type == "password") {
demoInput.type = "text";
demo_img.src = "invisible.png";
}else {
demoInput.type = "password";
demo_img.src = "visible.png";
}
}
</script>

Demo It was originally free, but the system may have increased it to 10 points as more people signed up. Here is a github demo download address. If you don’t have points, you can download it for free from here:

https://github.com /jiangxh1992/HTML5InputDemo

【Recommended learning: html video tutorial

The above is the detailed content of How to hide and display passwords in html. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn