JQuery / JavaScript MAC 地址验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12010552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery / JavaScript MAC Address Validation
提问by Pinch
Possible Duplicate:
What is a regular expression for a MAC Address?
可能的重复:
什么是 MAC 地址的正则表达式?
I would like to validate a string to ensure that it is a valid MAC Address.
我想验证一个字符串以确保它是一个有效的 MAC 地址。
Any ideas in Jquery or Javascript?
在 Jquery 或 Javascript 中有什么想法吗?
I have the following:
我有以下几点:
var mystring= '004F78935612' - This type of MAC Address
var rege = /([0-9a-fA-F][0-9a-fA-F]){5}([0-9a-fA-F][0-9a-fA-F])/;
alert(rege.test(mystring));
But its not all that accurate.
但它并不是那么准确。
Ie. My tissue box is a valid MAC Address?!?
IE。我的纸巾盒是有效的 MAC 地址?!?
Thanks!
谢谢!
回答by jbabey
Taking the regular expression from this question, you would implement it like so:
从这个问题中获取正则表达式,你会像这样实现它:
var mystring= 'Hello';
var regex = /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/;
alert(regex.test(mystring));
This regular expression searches for the beginning of the string ^
, then TWO hexidecimal digits [0-9A-F]{2}
, then a colon or dash [:-]
, five times over (...){5}
, then a final group of TWO hexidecimal digits [0-9A-F]{2}
, and finally the end of the string $
.
这个正则表达式搜索字符串的开头^
,然后是两个十六进制数字[0-9A-F]{2}
,然后是一个冒号或破折号[:-]
,五次以上(...){5}
,然后是最后一组两个十六进制数字[0-9A-F]{2}
,最后是字符串的结尾$
。
Edit: in response to your comment, Pinch, that format is not considered a valid MAC address. However, if you wanted to support it, simply add a ?
in the right place:
编辑:为了回应您的评论,Pinch,该格式不被视为有效的 MAC 地址。但是,如果您想支持它,只需?
在正确的位置添加一个:
/^([0-9A-F]{2}[:-]?){5}([0-9A-F]{2})$/
// question mark ^ allows the colon or dash to be optional