You are here: HomeForums » PHP » Regex » Using regex to validate IP address

Using regex to validate IP address (1 post)

in Forums » PHP » Regex
  • Started 1 month ago by thdadmin

thdadmin (administrator)

The idea is to validate an IP address according to an IP address' format. This little bit of code uses regex to verify the range of the numbers and the format:

<?php
$ip = "255.255.255.255";
if (preg_match(
'/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/',$ip))
{
echo "IP valid.";
}
else
{
echo "IP invalid";
}

I used a similar method for validating an email address. Also using regex.

Posted 1 month ago #

Reply

You must log in to post.