html input密码显示为“*”

本文介绍了一种方法,在HTML中将密码输入框的默认显示字符从实体圆点改为星号(*),通过使用两个input框实现,其中一个作为真正的密码输入框,另一个作为辅助显示框,实时更新星号数量。

1. 功能需求:HTML中,在input password输入框中输入字符将默认显示为“实体圆点”,但这里要求将实体圆点字符替换成“*”号显示。

2. 局限:鼠标光标非IE浏览器不一定显示,选择多个字符时未有视觉上的区分,功能没有影响;不支持中文输入。

3. 实现:通过两个input框实现,设置字符字体为等宽字体;其中真正的密码输入框设置为透明且遮盖住另一输入框,将非密码输入框的字符依密码字符串的多少显示“*”字符的个数。

附上测试文件如下:

<!DOCTYPE html>
<html encoding="utf-8">
<head>
	<style>
	*{margin:0;padding:0}
	input{font:14px Monospace;height:20px;width:160px;}
	label{display:inline-block;width:100px;height:20px;}
	#pass{position:absolute;left:100px;top:0;opacity:0;filter:alpha(opacity=0);z-index:2;}
	form{position:relative;}
	#hint_pass{position:absolute;left:100px;}
	</style>
</head>
<body>
	<form>
		<label>Password:</label>
		<input type="text" id="hint_pass" maxlength="20" tabindex="-1" />
		<input type="text" id="pass" name="pass" maxlength="20" />
	</form>	
<script>
	var pass=document.getElementById('pass');
	var hint_pass=document.getElementById('hint_pass');
	pass.onkeyup=pass.onchange=function(){hint_pass.value=pass.value.replace(/./g,'*');};
</script>	
</body>
</html>

转载于:https://www.cnblogs.com/susanws/p/5411951.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值