HTML5表单中password输入框的文字显示与隐藏实现

@问题描述与思路


HTML5表单中对于密码输入框password类型可以隐藏用户输入的内容,但有时候会用到允许用户自由显示或者隐藏输入框内容:要实现这个功能首先想到的是用js动态改变inputtype类型,即将type password变成type text隐藏的密码就会显示,但是实际上却实现不了,没有效果,所以,只能换一个思路:

[2017.5.19更新:最新的type设置已经奏效了,可以直接修改type=text和type=password来显示和隐藏密码输入框了,更新后的代码见下方,原方法请弃用]

放两个input,一个是password,一个是text,共同监听用户的输入事件,用户每次切换我们用js控制两个input的显示与隐藏来实现此效果。


实现步骤:

1.首先写好HTML界面标签以及css样式(其中的text的input开始先隐藏:style="display:none",后面显示和隐藏操作也通过改变display的属性来实现)

CSS:

<style type="text/css">
body{
	margin:0px;  
	background-color: white; 
	font-family: 'PT Sans', 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">
		<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">
		<input type="text" id="input_visible" placeholder="Password"/>
	</div>

	<button οnclick="">Login</button>
</div>

2.然后要用JS实现点击事件的交替操作:开始密码是隐藏的,点击后面的小眼睛图标显示密码,也就是把passwordinput隐藏然后把textinput显示出来,同时注意要把password的值传到text里面去,反过来一个道理:

JS:

<script type="text/javascript">
	// 这里使用最原始的js语法实现,可对应换成jquery语法进行逻辑控制
	var visible=document.getElementById('psw_visible');//text block
	var invisible=document.getElementById('psw_invisible');//password block
	var inputVisible = document.getElementById('input_visible');
	var inputInVisible = document.getElementById('input_invisible');
    //隐藏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>

Demo下载:http://download.csdn.net/detail/cordova/9713633


更新后的代码如下:

HTML:

<div id="page_container">
	<!--密码输入框-->
	<div class="input_block">
		<img id="demo_img" οnclick="hideShowPsw()" src="visible.png">
		<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下载:http://download.csdn.net/detail/cordova/9847089


Demo本来是免费的,可能下的人多了系统给提高到了10积分,这里再提供一个github的demo下载地址,没有积分的可以从这里免费下载:

  • 34
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
A:前端实现表单并验证输入框需要使用HTML、CSS和JavaScript语言。以下是一个简单示例: HTML代码: ```html <form id="myForm" action="#" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required><br><br> <label for="password">密码:</label> <input type="password" id="password" name="password" required><br><br> <button type="submit">提交</button> </form> ``` CSS代码: ```css input[type=text], input[type=password] { width: 100%; padding: 12px 20px; margin: 8px 0; box-sizing: border-box; } button[type=submit] { background-color: #4CAF50; color: white; padding: 12px 20px; border: none; cursor: pointer; } button[type=submit]:hover { background-color: #45a049; } ``` JavaScript代码: ```javascript const form = document.getElementById("myForm"); const username = document.getElementById("username"); const password = document.getElementById("password"); form.addEventListener("submit", function(event) { event.preventDefault(); // 阻止默认提交行为 if (username.value.trim() === "" || password.value.trim() === "") { alert("用户名和密码不能为空!"); return false; } else { alert("表单已提交!"); form.reset(); // 重置表单 return true; } }); ``` 以上代码实现了一个包含用户名和密码输入框表单,并在提交前验证了两个输入框的内容是否为空。如果为空,则弹出警告框,否则重置表单并弹出一个消息框。你可以在此基础上修改代码以实现其他验证功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_厚厚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值