Click to See Complete Forum and Search --> : ereg problems


prophit
11-24-2006, 07:28 PM
Trying to get this to validate the ip address. But for some reason the first and last octet doesnt work right. You can enter more then 3 numbers and it will work. The middle octets work perfectly. Not sure why?


if(!ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})", $_POST["ipaddr"]))

NogDog
11-24-2006, 08:05 PM
You'll need to anchor the pattern with beginning- and end-of-string markers, otherwise it's just checking for the specified pattern anywhere within the string, such as:

12123.12.123.123456

Therefore, you need something like the following for your regex:

"^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$"

prophit
11-24-2006, 08:27 PM
that worked perfectly!!! thanks!!!!!!!!