|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
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>";
}
?> | | |
|
| A Timing Class Categories : PHP, PHP Classes, 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 | | | Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip Categories : Email, Network, PHP, PHP Classes | | | Power Form Validation Categories : PHP, PHP Classes, Data Validation | | | Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides | | | Example of using the pcCalendar class, article 1468 on weberdev.com. Calendar example. Categories : PHP, Date Time, PHP Classes, Calendar | | | Open and Close your website in fixed times . Categories : PHP, PHP Classes, Cron, Date Time | | | 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 | | | A time measuring and performance benchmarking class Categories : PHP, PHP Classes, Testing, Debugging, Date Time | | | Three Cool Classes and One Trick Categories : PHP, PHP Classes, Graphics, Email | | | Find the day of the week for any given year/month/day. Categories : PHP, Date Time, Data Validation, Algorithms, Beginner Guides | | | Bs_StopWatch is a class to measure time intervals in microseconds.
Categories : PHP, Date Time, PHP Classes | | | PHP MIME Decoder. This class decodes Mime Encoded email message.
Attachments are stored in a director. Works with Multipart/alternative,
multipart/mixed etc.
see http://p3mail.com for example. Categories : PHP, PHP Classes, Email | | | Find if a year is leap. Categories : PHP, Date Time, Beginner Guides, Data Validation | | | Customizable Calendar Class Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar | |
|
|
|