标签:
<body>
账号:<input type="text"><span style="color: red"></span>
<p>密码:<input type="password"><span style="color: red"></span></p>
<p><button>登陆</button></p>
<script>
var usernameEle = document.getElementsByTagName("input")[0],
passwordEle = document.getElementsByTagName("input")[1],
usernameWarnEle = document.getElementsByTagName("span")[0],
passwordWarnEle = document.getElementsByTagName("span")[1],
btnEle = document.getElementsByTagName("button")[0];
usernameEle.onblur = function() {
var usernameVal = usernameEle.value;
//全部清空
usernameWarnEle.innerHTML = "";
if (!/^[\u4e00-\u9fa5]|[A-z]|[0-9]{4,20}$/.test(usernameVal)){
usernameWarnEle.innerHTML = "格式错误!";
}else{
usernameWarnEle.innerHTML = "格式正确!";
}
//判断
if (usernameVal === "") {
usernameWarnEle.innerHTML = "账号不能为空!";
}
}
passwordEle.onblur = function() {
var passwordVal = passwordEle.value;
//全部清空
passwordWarnEle.innerHTML = "";
//判断
if (passwordVal === "") {
passwordWarnEle.innerHTML = "密码不能为空!";
}
}
</script>
</body>
标签:
原文地址:http://www.cnblogs.com/huangmingkedeboke/p/5551165.html