Enforcing Data Rules
You will often want to ensure not only that data is entered into required fields but that the quality of the data is good enough before proceeding. For instance, you might want to check that an email address or a phone number has the right format, using the rules developed in Lesson 8, "Regular Expressions." You could also enforce a minimum length on a field to make sure a user cannot just enter an x in each field to continue to the next page.Listing 13.1 after the required fields check to enforce suitable values for email address and telephone number:
Because these additional rules add new messages to $err if an error is found, the rest of the script remains unchanged.
if ($_POST["email"] &&
!ereg("^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$",
$_POST["email"]))
$err .= "Email address format was incorrect <br>";
if ($_POST["telephone"] &&
!ereg("^\([[:digit:]]{3}\)[[:digit:]]{3}-[[:digit:]]{4}$",
$_POST["telephone"]))
$err .= "Telephone must be in format (555)555-5555 <br>";