0

I want to check the range of ip address in a regular expression , I was using this way and it's working so successfully

function validate_ip(ip)
{
       // See if x looks like an IP address using our "almost IP regex".
    var regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
    var match = regex.exec(ip);
    if (!match) return false;
    // Additional code to check that the octets aren't greater than 255:
    for (var i = 1; i <= 4; ++i) {
        if (parseInt(match[i]) > 255) 
            return false;
    }
    return true;
}

now i want to perform checking of the range and the syntax in just regular expression can this be done ?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Sally
  • 87
  • 2
  • 12

3 Answers3

5

The most straightforward approach is to look at the different cases:

25[0-5]|2[0-4]\d|1?\d\d?

This will match numbers between 0 and 255, disallowing prefixed zeroes such as: 055.

If you want to exclude zero:

25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?
phant0m
  • 16,595
  • 5
  • 50
  • 82
  • 1
    The latter one would allow `00`, seems you need `[1-9]\d?|1\d\d` – Bergi Jan 24 '13 at 14:51
  • thanks , it's working but neither the first or the second accepts zero before numbers e.g.055 give it a try pagecolumn.com/tool/regtest.htm – Sally Jan 26 '13 at 07:58
  • 1
    @Sally I don't know what you tried, but it works just fine on that site. Are you aware that both Bergi's and my RegEx only match *exactly one* number? – phant0m Jan 26 '13 at 08:46
2

The regex for digits representing numbers from 1 to 255 would look like this:

/[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]/
// next try, allowing 0:
/[1-9]?\d|1\d\d|2[0-4]\d|25[0-5]/

I think you will admit that using parseInt is much more readable, less errorprone and better maintainable.

It even could be shorter:

/^\d{1,3}(\.\d{1,3}){3}$/.test(ip) && ip.split(".").every(function(octet) {
    return parseInt(octet, 10) < 256;
});

(using ES5 every method, might need a shim for legacy browsers)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    Shouldn't `\{1,3}` be `\d{1,3}`? – phant0m Jan 24 '13 at 15:04
  • I tried your both regular expression and it only accepts tow digit numbers like 23.23.23.23 and any three digits number like 255 can not be matched give it a try ! http://www.pagecolumn.com/tool/regtest.htm – Sally Jan 26 '13 at 07:54
0

May late but, some one 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 digit has allowed
    255.0.0.y – only digit has allowed
    666.10.10.20 – digit must between [0-255]
    4444.11.11.11 – digit must between [0-255]
    33.3333.33.3 – digit must 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)  
}  
ErickBest
  • 4,586
  • 5
  • 31
  • 43