简单的JS 正则验证手机号码JavaScript
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, init
·
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 验证一个手机号:第一位是1,第二位是3-9,以后每一位都是0-9 -->
<input type="text" id="inputs"> <button>验证手机号</button>
<span id="spans"></span>
<script>
var inputs =document.getElementById('inputs');
var spans =document.getElementById('spans');
var btn =document.getElementsByTagName('button')[0];
var patt = /^[1][3-9][\d]{9}/
btn.onclick = function () {
if (patt.test(inputs.value)) {
spans.innerText = '验证成功'
} else{
spans.innerText = '验证失败';
spans.style.color = 'red'
}
}
</script>
</body>
</html>
更多推荐
已为社区贡献1条内容
所有评论(0)