Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 316
Manikandan Sethuraju Avatar asked Apr 04 '12 07:04

Manikandan Sethuraju


People also ask

What would be the regex to validate the IP address?

// this is the regex to validate an IP address. = zeroTo255 + "\\." + zeroTo255 + "\\."

How do I know if an IP address is valid?

A valid IP address must be in the form of A.B.C.D, where A,B,C and D are numbers from 0-255. The numbers cannot be 0 prefixed unless they are 0. Note:You only need to implement the given function.

What is ?! In regex?

The ?! n quantifier matches any string that is not followed by a specific string n.


2 Answers

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).

like image 107
Colin Hebert Avatar answered Oct 02 '22 12:10

Colin Hebert


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>
like image 36
ohaal Avatar answered Oct 02 '22 14:10

ohaal