Home Web Front-end HTML Tutorial How to hide and display passwords in html

How to hide and display passwords in html

Jun 03, 2021 am 11:17 AM
html

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>
Copy after login

HTML:

<div id="page_container">
<!--暗文密码输入框-->
<div class="input_block" id="psw_invisible">
<img  src="/static/imghw/default1.png"  data-src="visible.png"  class="lazy"  id="visible" οnclick="showPsw()"  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  src="/static/imghw/default1.png"  data-src="invisible.png"  class="lazy"  id="invisible" οnclick="hidePsw()"  alt="How to hide and display passwords in html" >
<input type="text" id="input_visible" placeholder="Password"/>
</div>
 
<button οnclick="">Login</button>
</div>
Copy after login

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>
Copy after login

The updated code is as follows:

HTML:

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

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>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles