Regular expression for IP Address Validation

i want to validate the value is valid IP Address or not..!

I Used to validate like

ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";

it's working fine, but when i give the values like 12345678 , its also return true.. How to solve this?


Solution 1:

There is a simpler way. You just need to split the string on . and check that every number is between 0 and 255.

Additionally, you can check for hexa and split on : for IPv6.


Just because I think it's funny:

^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))$

Here is a regex that should handle IPs (v4).

Solution 2:

Looking for one for IPv4, I ended up just creating it myself. (This only handles the common dotted variant, i.e. 0.0.0.0 - 255.255.255.255)

^                           # START OF STRING
  (?=\d+\.\d+\.\d+\.\d+$)     # Lookahead, require this format: number.number.number.number END OF STRING
  (?:                         # Start non-capture group (number 0-255 + optional dot)
    (?:                         # Start non-capture group (number 0-255)
      25[0-5]                     # 250-255
      |                           # OR
      2[0-4][0-9]                 # 200-249
      |                           # OR
      1[0-9]{2}                   # 100-199
      |                           # OR
      [1-9][0-9]                  # 10-99
      |                           # OR
      [0-9]                       # 0-9
    )                           # End non-capture group
    \.?                         # Optional dot (enforced in correct positions by lookahead)
  ){4}                        # End non-capture group (number + optional dot), repeat 4 times
$                           # END OF STRING

Without comments:

^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$

Some code to test it:

function isValidIpv4Addr(ip) {
  return /^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$/.test(ip);
}
var testAddr = ['192.68.35.35','0.0.0.0','255.0.0.0','192.168.1.0','192.168.0.1','255.255.255.0','1.1.1.1','255.255.255.255','249.249.249.249','200.200.200.200','199.199.199.199','100.100.100.100','99.99.99.99','0.0.0.0','9.9.9.9','10.10.10.10','99.99.99.99','100.100.100.100','109.109.109.109','110.110.110.110','199.199.199.199','200.200.200.200','249.249.249.249','250.250.250.250','255.255.255.255','256.256.256.260','192.168.0.0/24','192.168..1','192.168.1','1','1.','1.1','1.1.','1.1.1','1.1.1.','1.1.1.1.','1.1.1.1.1','.1.1.1.1','01.01.01.01','09.09.09.09','1.0.0.1.0','010.1.1.1','123456','123123123123','.127.0.0.1'];
for (var i = 0; i < testAddr.length; i++) {
  document.getElementById('ipv4tests').innerHTML += '<li>' + testAddr[i] + ' ' + (isValidIpv4Addr(testAddr[i]) ? '<font color="green">VALID!</font>' : '<font color="red">INVALID!</font>') + '</li>';
}
<ul id="ipv4tests"></ul>

Solution 3:

This works properly for all possible cases.

^(([1-9]?\d|1\d\d|2[0-5][0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|2[0-5][0-5]|2[0-4]\d)$

Solution 4:

I know this is old, but try this one:

    /^(?:(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)\.){3}(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)(?:\:(?:\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))?$/

I made it today for a function in php.

It handles ip's from 0.0.0.0 to 255.255.255.255 and ports from 0 to 65535.

Examples:

validates:
    0.0.0.0:0
    255.0.0.0
    192.168.1.0:8080
does not validate:
    192.168.0.0/24
    192.168..1
    192.168.1

I know this is a frankenregex, but still, it works!

If port doesn't matter, use this one:

    /^(?:(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)\.){3}(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)$/