A set of PHP form validation functions that use ereg() regular expressions to check that fields are filled in, an email address looks valid, and the basic format of a Hong Kong ID (HKID), phone number, and birth date. Run it before processing user input.
<?
function filled_out($form_vars)
{
// test that each variable has a value
foreach ($form_vars as $key => $value)
{
if (!isset($key) || ($value == ""))
// test suspended!!!!
return true;
}
return true;
}
function valid_email($address)
{
// check an email address is possibly valid
if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $address))
return true;
else
return false;
}
function valid_hkid($hkid_int, $hkid, $hkid_cd)
{
// check an HKID is possily valid
if ((ereg("[a-zA-Z]{1}", $hkid_int)) && (ereg("[0-9]{6}", $hkid)) && (ereg("[a-zA-Z0-9]{1}", $hkid_cd)))
return true;
else
return false;
}
function valid_phone($phone)
{
// check an HKID is valid
if (ereg("[0-9]", $phone))
return true;
else
return false;
}
function valid_bday($bday)
{
// check an HKID is valid
if (($bday < date("Y")-100) || ($bday > date("Y")-12))
return false;
else
return true;
}
?>Note: ereg() was deprecated in PHP 5.3 and removed in PHP 7 — new projects should use preg_match() instead.
