Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regular expression to check for IP addresses

I have several ip addresses like:

  1. 115.42.150.37
  2. 115.42.150.38
  3. 115.42.150.50

What type of regular expression should I write if I want to search for the all the 3 ip addresses? Eg, if I do 115.42.150.* (I will be able to search for all 3 ip addresses)

What I can do now is something like: /[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}/ but it can't seems to work well.

Thanks.

like image 234
KennC. Avatar asked Dec 16 '10 12:12

KennC.


People also ask

Which regex pattern can be used to find IP address?

Approach: Regex (Regular Expression) In C++ will be used to check the IP address. Specifying a range of characters or literals is one of the simplest criteria used in a regex.

Can I get IP address from JavaScript?

To get the client's public IP address, JavaScript acts as a third-party server-side language. JavaScript can't do it alone, so, jQuery is added to do this. JavaScript works with third-party applications to fetch the IP addresses.


2 Answers

May be late but, someone could try:

Example of VALID IP address

115.42.150.37 192.168.0.1 110.234.52.124 

Example of INVALID IP address

210.110 – must have 4 octets 255 – must have 4 octets y.y.y.y – only digits are allowed 255.0.0.y – only digits are allowed 666.10.10.20 – octet number must be between [0-255] 4444.11.11.11 – octet number must be between [0-255] 33.3333.33.3 – octet number must be between [0-255] 

JavaScript code to validate an IP address

function ValidateIPaddress(ipaddress) {     if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {       return (true)     }     alert("You have entered an invalid IP address!")     return (false)   }   
like image 61
ErickBest Avatar answered Sep 19 '22 18:09

ErickBest


Try this one, it's a shorter version:

^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$ 

Explained:

^ start of string   (?!0)         Assume IP cannot start with 0   (?!.*\.$)     Make sure string does not end with a dot   (     (     1?\d?\d|   A single digit, two digits, or 100-199     25[0-5]|   The numbers 250-255     2[0-4]\d   The numbers 200-249     )   \.|$ the number must be followed by either a dot or end-of-string - to match the last number   ){4}         Expect exactly four of these $ end of string 

Unit test for a browser's console:

var rx=/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/; var valid=['1.2.3.4','11.11.11.11','123.123.123.123','255.250.249.0','1.12.123.255','127.0.0.1','1.0.0.0']; var invalid=['0.1.1.1','01.1.1.1','012.1.1.1','1.2.3.4.','1.2.3\n4','1.2.3.4\n','259.0.0.1','123.','1.2.3.4.5','.1.2.3.4','1,2,3,4','1.2.333.4','1.299.3.4']; valid.forEach(function(s){if (!rx.test(s))console.log('bad valid: '+s);}); invalid.forEach(function(s){if (rx.test(s)) console.log('bad invalid: '+s);}); 
like image 44
oriadam Avatar answered Sep 22 '22 18:09

oriadam