You could use my validater class...I wrote it up quick for work. Im sure ill tweak it as I go but it works great for me.
<?php
/*
FILE: Validate.php
DESCRIPTION: A validation utility that takes values and checks them against constraits. Will return an array or errors if an error occured.
FUNCTIONS: Validate, SetNullMsg, SetEmailMsg, SetNumericMsg, CheckNull, CheckEmail, CheckNumeric, CheckRange, GetErrors, Reset, CheckArray, CheckDate, CheckMySqlDate, Override
*/
class Validate
{
var $errors;
var $null_msg;
var $email_msg;
var $numeric_msg;
var $date_msg;
var $phone_msg;
var $valid;
function Validate()
{
$this->errors = array();
$this->null_msg = "Required Field";
$this->numeric_msg = "Invlaid Number";
$this->email_msg = "Invalid Email";
$this->phone_msg = "Invalid Phone Number";
$this->date_msg = "Invalid Date";
$this->valid = true;
}
function SetNullMsg($msg)
{
$this->null_msg = $msg;
return true;
}
function SetNumericMsg($msg)
{
$this->numeric_msg = $msg;
return true;
}
function SetEmailMsg($msg)
{
$this->email_msg = $msg;
return true;
}
function SetPhonelMsg($msg)
{
$this->phone_msg = $msg;
return true;
}
function CheckNull()
{
$total_fields = func_num_args();
$field_list = func_get_args();
if (($total_fields % 2) != 0)
return false;
$ok = true;
for($i=1; $i < $total_fields; $i+=2)
{
if (empty($field_list[$i]))
{
$ok = false;
$this->errors[$field_list[$i-1]] = $this->null_msg;
$this->valid = false;
}
}
return $ok;
}
function CheckEmail()
{
$total_fields = func_num_args();
$field_list = func_get_args();
if (($total_fields % 2) != 0)
return false;
$ok = true;
for($i=1; $i < $total_fields; $i+=2)
{
if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $field_list[$i]))
{
$ok = false;
$this->errors[$field_list[$i-1]] = $this->email_msg;
}
list($Username, $Domain) = split("@",$field_list[$i]);
if(!getmxrr($Domain, $MXHost))
{
$ok = false;
$this->errors[$field_list[$i-1]] = $this->email_msg;
$this->valid = false;
}
}
return $ok;
}
function CheckNumeric()
{
$total_fields = func_num_args();
$field_list = func_get_args();
if (($total_fields % 2) != 0)
return false;
$ok = true;
for($i=1; $i < $total_fields; $i+=2)
{
if (!is_numeric($field_list[$i]))
{
$ok = false;
$this->errors[$field_list[$i-1]] = $this->numeric_msg;
$this->valid = false;
}
}
return $ok;
}
function CheckRange($name, $value, $lower, $upper, $msg="Invalid Length")
{
if (!is_numeric($lower) and !is_numeric($upper) and !empty($name) and !empty($value))
return false;
else
{
$length = strlen($value);
if (($length >= $lower) and ($length <= $upper))
return true;
else
{
$this->errors[$name] = $msg;
$this->valid = false;
return false;
}
}
}
function CheckPhone()
{
$total_fields = func_num_args();
$field_list = func_get_args();
if (($total_fields % 2) != 0)
return false;
$ok = true;
for($i=1; $i < $total_fields; $i+=2)
{
if (!eregi("^[2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4}$", $field_list[$i]))
{
$ok = false;
$this->errors[$field_list[$i-1]] = $this->phone_msg;
$this->valid = false;
}
}
return $ok;
}
function CheckArray($name, $value, $msg="Invalid Selection", $count=0)
{
if (!is_array($value))
{
$this->errors[$name] = $msg;
$this->valid = false;
return false;
}
else
{
if (count($value) == $count)
{
$this->errors[$name] = $msg;
$this->valid = false;
return false;
}
}
return true;
}
function CheckDateS($name, $month, $day, $year)
{
if (!@checkdate($month, $day, $year))
{
$ok == false;
$this->errors[$name] = $this->date_msg;
$this->valid = false;
}
}
function GetErrors()
{
return $this->errors;
}
function Reset()
{
$this->errors = array();
return true;
}
function IsValid()
{
return $this->valid;
}
function Override($value)
{
$this->valid = $value;
return true;
}
}
?>
I use it like this...
if ($POST["submit"])
{
require_once("validate.php");
$validate = new Validate();
$validate->CheckNull("fname", $POST["fname"], "lname", $_POST["lname"]);
if ($validate->IsValid())
{
// do inserts
}
else
$errors = $validate->GetErrors();
....
then a form field would look like this
<input type="text" name="fname" value="<?php echo $_POST["fname"]; ?>"><?php echo $errors["fname"]; ?>
<input type="text" name="lname" value="<?php echo $_POST["lname"]; ?>"><?php echo $errors["lname"]; ?>
as you can see the function take an even number of arguments the first of a set being the returned array key...so passing to...CheckNull("my_name", $fname)
would set $errors["my_name"] = "Error Message";
and everythin is passed like that in sort of a set manner. There are other functions like checkdate, checkarray, etc. that you can look at. Some work the same way while other only take a single form argument instead of multiple. Likewise you can set the error message for the functions that take multiple form fields with the approriate set function.
so in the example above to set the error message for the null messages would be...
$validate->SetNullMsg("Entry Not Good")
}
would set $errors["my_name"] = "Entry Not Good"
that is if there was an error.
🙂