This class can be used for validating email address and also dates. It return TRUE if the email address or date is valid else it returns FALSE.
validator.php
<?php
class Validator{
var $reportError = FALSE;
#===========================================================================#
# * Class Constructor #
# * @param : [$reportError] Whether to report an error or not #
#===========================================================================#
function Validator($reportError = FALSE){
$this->reportError = $reportError;
}
#===========================================================================#
# * Function for checking the validity of an email address #
# * @param : ($email ) Email ID to be validated #
# * @param : [$reportError] Whether to report an error or not #
# * @return : TRUE if valid else FALSE #
#===========================================================================#
function validEmail($email, $reportError = ''){
$isValid = FALSE;
// If $reportError is not set set it
if($reportError == ''){
$reportError = $this->reportError;
}
// Check whether the given Email Address is Empty or not
if(empty($email)){
// Report the Error
$this->_reportError('Email ID is empty', $reportError);
}else{
// Check whether the given Email Address is Valid or not
if(eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}\$", $email)) {
$isValid = TRUE;
}else{
// Report the Error
$this->_reportError('Invalid Email ID', $reportError);
}
}
// Return TRUE if the ID is valid, else FALSE
return $isValid;
}
#===========================================================================#
# * Function for checking the validity of a date #
# * @param : ($day ) Day #
# * @param : ($month ) Month #
# * @param : ($year ) Year #
# * @param : [$reportError] Whether to report an error or not #
# * @return : TRUE if valid else FALSE #
#===========================================================================#
function validDate($day, $month, $year, $reportError = ''){
$isValid = FALSE;
$arDayNo = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
// If $reportError is not set set it
if($reportError == ''){
$reportError = $this->reportError;
}
// Check whether any field is empty
if(empty($day) || empty($month) || empty($year)){
// Report the Error
if(empty($day))$this->_reportError('Day is empty', $reportError);
if(empty($month))$this->_reportError('Month is empty', $reportError);
if(empty($year))$this->_reportError('Year is empty', $reportError);
}else{
// Check whether the given year is Leap or Not
if((!($year % 4) && ($year % 100)) || !($year % 400)){
$arDayNo[1] = 29;
}
// Check whether the given date is valid or not
if(($day > 0 && $day <= $arDayNo[$month - 1]) && ($month > 0 && $month <= 12) && $year > 0)
$isValid = TRUE;
else{
$this->_reportError('Invalid Day', $reportError);
}
}
}
#===========================================================================#
# * Function for reporting / echoing the Error #
# * @param : ($errorMsg ) The Error Message #
# * @param : [$reportError] Whether to report an error or not #
# * @return : TRUE if valid else FALSE #
#===========================================================================#
function _reportError($errorMsg, $reportError = ''){
// If $reportError is not set set it
if($reportError == ''){
$reportError = $this->reportError;
}
if($reportError)echo "<b>Error</b>: $errorMsg<br>";
}
}
?>
Usage Exmaple
<?php
/*
Function
* To validate an email addresses
* To validate dates
How to use
* Include the class file [Eg: include 'validator.php';]
* Create an instance of the class [Eg: $v = new Validator();]
If Optional boolean value is set then errors will be reported, if not
overridden in the function [Eg: $v = new Validator(TRUE);]
* Call the function with the required arguments
[Eg : $v->validEmail('me@mydomain.com');
If the optional fourth argument is set it will override the default
Error Reporting option [$v->validEmail('me@mydomain.com', TRUE);]
$v->validDate(1, 1, 2007);
If the optional fourth argument is set it will override the default
Error Reporting option [$v->validDate(1, 1, 2007, FALSE);]
]
*/
// Include the class file
include 'validator.php';
// Create an instance of the class
$v1 = new Validator;
$email = 'me@mydomain.com';
if($v->validEmail($email)){
echo "The Email ID is valid<br>";
}else{
echo "The Email ID is invalid<br>";
}
$day = 25;
$month = 12;
$year = 2006;
if($v->validDate($day, $month, $year)){
echo "The date is valid<br>";
}else{
echo "The date is invalid<br>";