Jump to content

Validation


OriginalSunny

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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.