PHP Regex Validate IP address


Before we see the code, let’s first understand what an IP address is consist of. IP address is four groups of numbers between 0 and 255 separated by periods. The address 192.168.0.1 is a valid IP address, but 256.0.1.2 isn’t.

You can use the code to validate an IP address.

<html>
<head><title>Validating IP addresses</title></head>
<style>
          .err { color : red ; font-weight : bold }
</style>
<body>
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="input" /><br/>
<input type="submit" value="Submit Form" /><br/><br/>
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
    $input = $_POST['input'];
	if (preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $input))
{
        print "valid!";
    }
    else
    {
        print "<span class=\"err\">Bad IP address. Please correct and " .
            "resubmit the form</span><br/>";
    }
}
?>
</form>
</body>
</html>


Regular Expression Explanation:
The bulk of this expression is a group that breaks down the numbers that range from 0 to 255. The expression would be a lot shorter if 002 or 015 were valid instead of 2 and 15, respectively, but for this expression you want to specify IP addresses without the leading zeros.

The range from 0 to 255 breaks down into other ranges: 0–99, 100–199, 200–249, and 250–255. The expression to match this is ([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]), which can be broken down into [1-9]?[0-9], which will match 0–99; 1[0-9]{2}, which will match 100–199; 2[0-4][0-9], which will match 200–249; and 25[0-5], which will match 250–255.

After taking out the IP address validation expression, the rest of it breaks down like this:

^

the beginning of the line

(

the beginning of a group that contains

( )

the IP address expression explained previously

\.

a literal dot

)

the end of the group

{3}

occurring exactly three times

( )

another occurrence of the IP address

$

the end of the line.

hope it helps!

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

  1. #1 by Josh (vancouver computers) on August 24, 2008 - 10:19 pm

    Hi,

    I am looking for a regex validator for an email form, to validate the email address.

    Would appreciate any help.
    Cheers

  2. #2 by admin on August 25, 2008 - 7:32 am

    Hi Josh

    I have written a JavaScript email validator for you at: http://www.blog.highub.com/javascript-advanced/javascript-validating-email-address/

    do you need a PHP version?

  3. #3 by Josh (vancouver computers) on September 14, 2008 - 4:54 pm

    Thank you very much. A PHP code would be easier to work with if you have it. Thanks again.

  4. #4 by admin on September 17, 2008 - 8:17 am

    Hi, Josh

    I have written a PHP version with detailed explaination for you at: http://www.blog.highub.com/regular-expression/php-regular-expression/validate-email-address/

  5. #5 by me on September 29, 2008 - 9:50 am

    This is pretty worthless unless it has ipv6 validation too. Also your explanation of an IP address will confuse people because a v6 address DOES NOT consist of “four groups of numbers between 0 and 255 separated by periods”. ;-)

  6. #6 by Dave on January 14, 2009 - 12:54 pm

    You forgot to escape the dot in your regex. This means that as written it will accept 1x1x1x1 as well as 1.1.1.1

    The correct regex is /^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/ (hopefully the code doesn’t get munged in the web textedit box)

  7. #7 by Fiji Web Design on January 21, 2009 - 4:59 pm

    “This is pretty worthless unless it has ipv6 validation too.”

    It is pretty clear that this is IPv4 email address validation and when you say IP, you generally mean IPv4.

    Here’s a similar regular expression I saw on php.net a while ago:
    /^(1?\d{1,2}|2([0-4]\d|5[0-5]))(\.(1?\d{1,2}|2([0-4]\d|5[0-5]))){3}$/

    http://www.php.net/manual/en/function.preg-match.php

(will not be published)

Comments are closed.