WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Validator - A PHP class that can can be used for validating Email IDs and Dates
Categories : PHP, PHP Classes, Data Validation, Email, Date Time Click here to Update Your Picture
Joyce Babu
Date : Jul 01st 2006
Grade : 3 of 5 (graded 4 times)
Viewed : 8039
File : 4435.zip
Images : No Images for this code example.
Search : More code by Joyce Babu
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

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>";

}
?>



Open and Close your website in fixed times .
Categories : PHP, PHP Classes, Cron, Date Time
Class that allows the PHP developer to establish connections with a POP3 mail server amd be able to list, retrieve and delete mail messages from a given mail box.
Categories : Network, Email, PHP, PHP Classes
A time measuring and performance benchmarking class
Categories : PHP, PHP Classes, Testing, Debugging, Date Time
Banknote Validation - A PHP class that provides several methods to quickly validate banknote serial numbers of the following currencies: AUD, CAD, CHF, CNY, EUR, GBP, JPY, USD.
Categories : PHP, PHP Classes, Data Validation, Regexps
Find the day of the week for any given year/month/day.
Categories : PHP, Date Time, Data Validation, Algorithms, Beginner Guides
Power Form Validation
Categories : PHP, PHP Classes, Data Validation
Find if a year is leap.
Categories : PHP, Date Time, Beginner Guides, Data Validation
Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides
Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes
A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Categories : PHP, PHP Classes, Calendar, Date Time
pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes
a class that uses microtime() to provide easy calculation of elapsed times
Categories : Date Time, PHP, PHP Classes
Bs_StopWatch is a class to measure time intervals in microseconds.
Categories : PHP, Date Time, PHP Classes
PHP Image Validation Class - test if a specific file is of a certain image type without relying on the said file extension.
Categories : PHP, PHP Classes, Data Validation, Graphics, Beginner Guides
EAvalidator - This class can be used to validate an e-mail address by checking its domain.
Categories : PHP, PHP Classes, Email, Regexps