简单的检测输入的手机号码是否正确

来源:互联网 发布:玲珑密保网络错误 编辑:程序博客网 时间:2024/06/05 08:33
<style type="text/css">
.box {
padding: 50px;
}

.left,
.tip {
float: left;
}

.left {
margin-right: 10px;
}

.tip {
display: none;
font-size: 14px;
}
</style>
<script>
window.onload = function() {
var phone = document.getElementById("phone");
var tip = document.getElementById("tip");
phone.onfocus = function() {
tip.style.display = "block";
}
phone.onblur = function() {
var phoneVal = this.value; 
if (phoneVal.length == 11 && isNaN(phoneVal) == false) { 
tip.innerHTML = "输入正确";
} else {
tip.innerHTML = "输入错误";
}
}
}
</script>
</head>

<body>
<div class="box">
<div class="left">
<input type="text" id="phone" placeholder="请输入你的手机号码">
</div>
<div class="tip" id="tip">
请输入有效的手机号码
</div>
</div>
</body>