检查用户输入(加提示),高亮显示

55 篇文章 0 订阅

JavaScript操作表单属性②

  1. 检查用户输入,高亮显示
    要求:用户名不超过10个字符,不低于5个字符
    css部分
<style>
input {
	height: 40px;
	width: 300px;
	padding-left: 5px;
	border-radius: 5px;
	outline: none;
	border: 1px solid #eee;
}
label {
	position: absolute;
	top: 20px;
	left: 68px;
	color: #ccc;
	cursor: text;
}
p {
	margin-left: 47px;
	font: 14px/30px "microsoft yahei";
	color: #F00;
	display: none;
}
.wrong {
	outline: none;
	border: 1px solid #F00;
}
.right {
	outline: none;
	border: 1px solid #0F0;
}
</style>

body部分

账号:<label for = "inp">请输入用户名:</label><input type="text" id="inp" value="">
<p>提示:用户名的长度需在5-12之间</p>
<script>
var inp = document.getElementsByTagName("input")[0];
var lab = document.getElementsByTagName("label")[0];
var p = document.getElementsByTagName("p")[0];
inp.focus();
inp.oninput = function (){
	lab.style.display = "none";
	if(inp.value.length <5 || inp.value.length>12){
		inp.className = "wrong";
		p.style.display = "block";
	}
	if(inp.value.length>=5&&inp.value.length<=12){
		inp.className = "right";
		p.style.display = "none";
	}
}
inp.onblur = function(){
	if(inp.value == ""){
		lab.style.display = "block";
		inp.className = "";
		p.style.display = "none";
	}
}
</script>

效果如下图输入字符太少
输入的字符刚刚好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值