Jun
25
PHP Regex Validate IP address
June 25, 2008 |
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!
Similar Posts
- PHP Regex - Validate Email Address
- PHP Regex Extract Username from Email Address
- Validate URL Using PHP Regex
- PHP Regex Extract Directiory from Full Path
- PHP Regex Extract Filename from Full Path
- PHP Regex - Extract Filenames from Full Path
- PHP Regex Remove Whitespace from HTML
Comments
5 Comments so far



































Hi,
I am looking for a regex validator for an email form, to validate the email address.
Would appreciate any help.
Cheers
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?
Thank you very much. A PHP code would be easier to work with if you have it. Thanks again.
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/
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”. ;-)