2

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
  • 9
    Rather than trying to make a more complex regex, why not just first check the input for the values ff:ff:ff:ff:ff:ff and 00:00:00:00:00:00, and then if it is neither of those, run it through the regex? 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 !=). 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 and 00: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 compltely 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. Commented Aug 20, 2013 at 13:26
  • 1
    stackoverflow.com/questions/4260467/… 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. Commented Aug 20, 2013 at 13:36

5 Answers 5

4
var 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); 
Sign up to request clarification or add additional context in comments.

Comments

1
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 

Comments

0

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; } 

Comments

0

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 completion

Comments

-1

My answer is:

^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F][1-9a-eA-E]$ 

1 Comment

Why must only the last digit not be 0 or f? What about any of the other digits? That regex ends up not matching on any MAC address that ends in 0 or f (12:34:56:78:9a:bf, for instance, would not be matched), rather than excluding the case of all 0s or all fs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.