Jump to content

Archived

This topic is now archived and is closed to further replies.

OriginalSunny

Validation

Recommended Posts

Hi,
I don't quite understand the following piece of code.
[i]
if(eregi("zip",$field))
{
if(!ereg("^[0-9]{5,5}(\-[0-9]{4,4})?$",$value))
{
$errors[] = "$value is not a valid zipcode.";
}
}
if(eregi("phone",$field))
{
if(!ereg("^[0-9)(xX -]{7,20}$",$value))
{
$errors[]="$value is not a valid phone number. ";
}
}[/i]

I know its the fields are being validated, but all i get for the 'zip' field is that numbers have to be between 0 and 9 and you can use ^. I dont understand what the rest of it is doing. If someone could explain it to me. The same goes for the 'phone' field. Thanks.

Share this post


Link to post
Share on other sites
^ means match the beginning of a string. [0-9]{5,5} mean match any character in the 0-9 range a minimum of 5 times and a maximum of 5 times. So match a string of 5 numbers. \- means match the - character, the \ escapes it. Then match 4 0-9's. $ matches the end of the string.

so ^12345-1234$, looks like this 12345-1234 as a string.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.