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?


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|1dd|2([0-4]d|5[0-5])).(d|[1-9]d|1dd|2([0-4]d|5[0-5])).(d|[1-9]d|1dd|2([0-4]d|5[0-5])).(d|[1-9]d|1dd|2([0-4]d|5[0-5]))$

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


这适用于所有可能的情况。

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

Looking for one for IPv4, I ended up just creating it myself. (This only handles the common dotted variant, ie 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>
链接地址: http://www.djcxy.com/p/86984.html

上一篇: 正则表达式从字符串的末尾获取数字

下一篇: IP地址验证的正则表达式