Can anyone send javascript code to validate the network mac address (eg. 02:41:6d:22:12:f1
) It accepts value from 00:00:00:00:00:00
to ff:ff:ff:ff:ff:ff
. On keypress event of textbox, I need to allow characters 0-9
, a-f
and :
(colon). What I have so far is
macPattern = /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i;
With this I am able throw exception for ff:ff:ff:ff:ff:ff
but I also need to throw an exception for 00:00:00:00:00:00
. My pattern is not throwing an exception.
Could you please give me a pattern through which I should able to throw an exception for both ff:ff:ff:ff:ff:ff
and 00:00:00:00:00:00
.
Can anyone send javascript code to validate the network mac address (eg. 02:41:6d:22:12:f1
) It accepts value from 00:00:00:00:00:00
to ff:ff:ff:ff:ff:ff
. On keypress event of textbox, I need to allow characters 0-9
, a-f
and :
(colon). What I have so far is
macPattern = /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i;
With this I am able throw exception for ff:ff:ff:ff:ff:ff
but I also need to throw an exception for 00:00:00:00:00:00
. My pattern is not throwing an exception.
Could you please give me a pattern through which I should able to throw an exception for both ff:ff:ff:ff:ff:ff
and 00:00:00:00:00:00
.
-
9
Rather than trying to make a more plex regex, why not just first check the input for the values
ff:ff:ff:ff:ff:ff
and00:00:00:00:00:00
, and then if it is neither of those, run it through the regex? – ajp15243 Commented Aug 20, 2013 at 13:12 - 2 I would just use the regex as is, and add a quick check for the two known exceptions (just using !=). – fred02138 Commented Aug 20, 2013 at 13:13
-
1
A regular expression is not synonymous with "do all the logic needed to validate input." If you have a requirement like excluding
ff:ff:ff:ff:ff:ff
and00:00:00:00:00:00
, it would be much better to exclude those cases using ordinary programming logic as @ajp15243 suggested twice, rather than "could you please give me pltely pattern." P.S. Is someone telling you that you have to do it all in a single regular expression? If so, who and why? Is this homework? This definitely doesn't seem to meet any real-world standard for ensuring input is an actual MAC address. – Joseph Myers Commented Aug 20, 2013 at 13:26 - 1 stackoverflow./questions/4260467/… – Joseph Myers Commented Aug 20, 2013 at 13:30
- 1 After some checking, I see that there are 281,474,976,710,656 possible valid MAC addresses. That is exactly 2^48, which means that both of the cases you are trying to exclude are theoretically possible. – Joseph Myers Commented Aug 20, 2013 at 13:36
5 Answers
Reset to default 4var MACAddress = document.getElementById("MACAddress");
var MACRegex=new RegExp("^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$");
MACRegex.test(MACAddress);
var re = /^(?!(?:ff:ff:ff:ff:ff:ff|00:00:00:00:00:00))(?:[\da-f]{2}:){5}[\da-f]{2}$/i;
// ^------------blacklist------------^ ^----------pattern---------^
re.test('ff:ff:ff:ff:ff:ff'); // false
re.test('ff:ff:ff:ff:ff:fe'); // true
re.test('00:00:00:00:00:00'); // false
re.test('00:00:00:00:00:01'); // true
// and of course
re.test('00:00:00:00:01'); // false
re.test('00:00:00:00:00:0g') // false
Oh if we are answering about validating mac, here is mine: but this still doesn't answer the question: how to prevent characters that don't make up a mac address.
function isValidMac(mac) {
var a = mac.split(':');
if (a.length !== 6) {
return false;
}
for (var i=0; i<6; i++) {
var s = "0x"+a[i];
if (s>>0 === 0 || s.length != 4) {
return false;
}
}
return true;
}
I think that while the other answers are all valid, they missed the keypress aspect of the OP's question. While that might not be important in this instance, I believe that the UX can be improved.
I would suggest;
-validating length =12
-accepting {0-9,a-f,A-F},
-alert {g-z,G-Z) (invalid character)
-ignore all others (including Tab, cr, lf, crlf)
-confirm exit after the 12th character
-display 3 forms; raw, couplet, quad
I have not yet had a chance to code but will submit and amend on pletion
My answer is:
^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F][1-9a-eA-E]$