0

I'm working with a backend web, in which I have to validate mac address before saving it to database. What is the regex of mac address?

I have tried every single regex in this question but it does not work well

The regrex I'm currently using is /(\A(([0-9A-Fa-f]{2}[:]){5}[0-9A-Fa-f]{2})|\A(([0-9A-Fa-f]{2}[-]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}.){2}[0-9A-Fa-f]{4})\z)/

It can validate mac address according to IEEE 802 standards such as 1a:1b:1c:1d:1e:1f or 1a-1b-1c-1d-1e-1f or 1234.qwer.asdf.zxcv

1a:1b:1c:1d:1e-1f return false.

But when I add any character after a correct string such as 1a:1b:1c:1d:1e:1fabcdefghi, it still return true as well. How to fix the regex to return false when facing with "1a:1b:1c:1d:1e:1fabcdefghi"?

Edit: add "(" and ")" as in @Wiktor's comment. It works

Hùng Nguyễn
  • 599
  • 8
  • 24

0 Answers0