Required fields
Some of your fields will be required. In other words, visitors have to enter something in the field. The following script checks that a first name was entered:
if (ereg(".", $first_name) == 1)
{
echo "First name: ", "$first_name";
$verify = "OK";
}
else
{
print ("<b>Error:</b> A first name is required.");
$verify = "bad";
}
ereg means "evaluate regular
expression". "Regular expressions" are the UNIX function for finding patterns in strings of letters and numbers. ereg is followed by parentheses, and you can put three arguments in the parentheses. The arguments are separated by commas. The first argument is the pattern to search for, usually surrounded by quotation marks. The second argument is where ereg is to search, usually a variable. The third, optional, argument is an array to put matches into. This argument is a variable. ereg returns either a "0" (false) or a "1" (true).
The dot . or period is a regular expression wild card meaning "any character."
(ereg(".", $first_name) == 1) means "the variable '$first_name' contains anything". If this expression is true, then the first name is printed, and the variable $verify is set to "OK".
The else argument executes when ereg returns "0" (false).
There are three other versions of the ereg function.
ereg_replace uses three arguments: the first is the pattern to search for, the second is the pattern to replace the first pattern, and the third is where to search (a variable). eregi is the same as ereg, except that it's not case-sensitive (i.e., it doesn't differentiate upper- and lower-case letters). eregi_replace is not case sensitive
Alternative means to the same end The line if (ereg(".", $first_name) == 1) can be simplified to if ($first_name). I used the longer form to show how to use ereg in a simple example.
Checking e-mail addresses
The following ereg arguments test validity of e-mail addresses: