1

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.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
Manu
  • 59
  • 1
  • 5
  • 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? – ajp15243 Aug 20 '13 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 Aug 20 '13 at 13:13
  • The pattern you tried matches ff:ff:ff:ff:ff:ff and 00:00:00:00:00:00? Or does it match on only one of them? What do you want the pattern to match? (I thought, the - in the [:-] has to be escaped, but I'm not sure) – urzeit Aug 20 '13 at 13:13
  • i need pattern which will through an exception for both case...but the one i am using only case of ff:ff:ff:ff:ff:ff throughing an exception not for 00:00:00:00:00:00 – Manu Aug 20 '13 at 13:16
  • @fred02138: could you please give me compltely pattern – Manu Aug 20 '13 at 13:19
  • @Manu You don't need to alter the regex, just put an `if` statement or two before the regex check that explicitly checks for the all `f` and all `0` MAC addresses, and do your error handling for those two cases within that `if` statement's code block. – ajp15243 Aug 20 '13 at 13:21
  • 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. – Joseph Myers Aug 20 '13 at 13:26
  • 1
    http://stackoverflow.com/questions/4260467/what-is-a-regular-expression-for-a-mac-address – Joseph Myers Aug 20 '13 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 Aug 20 '13 at 13:36

5 Answers5

3
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);
Sagar Vaghela
  • 1,165
  • 5
  • 20
  • 38
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
Paul S.
  • 64,864
  • 9
  • 122
  • 138
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;
}
Prasanth
  • 5,230
  • 2
  • 29
  • 61
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

-1

My answer is:

^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F][1-9a-eA-E]$
  • 1
    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 `0`s or all `f`s. – ajp15243 Aug 20 '13 at 14:08