JS - 正则表达式验证MAC地址(附样例)
作者:hangge | 2016-11-12 09:10
设备的 mac 地址(物理地址)的格式为:xx:xx:xx:xx:xx:xx。其中 xx 为16进制数字。下面通过样例演示如何使用正则判断,来验证 mac 地址是否正确。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hangge.com</title>
</head>
<script>
//mac网络地址验证
function check(){
var value = document.getElementById("mac").value;
var reg=/^[A-Fa-f\d]{2}:[A-Fa-f\d]{2}:[A-Fa-f\d]{2}:[A-Fa-f\d]{2}:[A-Fa-f\d]{2}:[A-Fa-f\d]{2}$/;
if(!reg.test(value)){
alert("mac地址不正确!");
}else{
alert("验证成功!");
}
}
</script>
<body>
<input type="text" id="mac" placeholder="mac地址"/>
<button type="button" onclick="check()">验证</button>
</body>
</html>
全部评论(0)