Location: PHPKode > scripts > Validate form elements > Class.Validate.php
<?php
/**

 @desc Class to validate form elements 
 @Osama Salama
 @copyright 2011,2012
 @Email hide@address.com
 @Version 0.1
 */

class Validate {

//@var  _instance Boolean single instance of a class
 protected static $_instance = null;
 //@var	errors   Array Error collection
 public $errors = array();
 

  public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

 /**
   * validate::Valid() -method To Validate data input 
   *@param var  Type:String  Form Elements submitted
   *@return array
   */

public function Valid($var){

if(is_array($var)){

foreach($var as $key=>$val){
 
switch($val['Type']){

case "Empty";
$result=$this->Rule_Empty($val['Var'],$val['Msg'],$val['required']);
if(isset($result)){
$errors[]=$result;
}

break;
case "Email";
$result=$this->Rule_Email($val['Var'],$val['Msg'],$val['required']);
if(isset($result)){
$errors[]=$result;
}
break;
case "IP";
$result=$this->Rule_IP($val['Var'],$val['Msg'],$val['required']);
if(isset($result)){
$errors[]=$result;
}
break;
case "Url";
$result=$this->Rule_Url($val['Var'],$val['Msg'],$val['required']);
if(isset($result)){
$errors[]=$result;
}
break;
case "Int";
$result=$this->Rule_Int($val['Var'],$val['Msg'],$val['required']);
if(isset($result)){
$errors[]=$result;
}
break;
case "String";
$result=$this->Rule_String($val['Var'],$val['Msg'],$val['required']);
if(isset($result)){
$errors[]=$result;
}
break;

case "Extentions":
$result=$this->Rule_Extentions($val['Var'],$val['Msg'],$val['Mime'],$val['required']);
if(isset($result)){
$errors[]=$result;
}
break;


case "Upload":
$result=$this->Rule_Upload($val['Var'],$val['Msg'],$val['required']);
if(isset($result)){
$errors[]=$result;
}
break;
}
}




return $errors;
} 
else {
return false;
}
}

/*
*
*
*
*
*/


public function Rule_Empty($var,$msg,$required){
 
$s=strlen($var);
if ($s <=0) {
return $msg;
 
} 
}


/*
*
*
*
*
*/

public function Rule_Email($var,$msg,$required){
//^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$
if($required==true){
$Valid=(bool) preg_match('/^(.+)@([^@]+)$/',$var);
if($Valid==false){
return $msg;
} 
}
if( (!empty($var) && $required==false)){
$Valid=(bool) preg_match('/^(.+)@([^@]+)$/',$var);
if($Valid==false){
return $msg;
} 
}
}



/*
*
*
*
*
*/


public function Rule_IP($var,$msg,$required){
if($required==true){
$Valid=(bool) preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $var);
if($Valid==false){
return $msg;
}
}

if( (!empty($var) && $required==false)){
$Valid=(bool) preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $var);
if($Valid==false){
return $msg;
} 
}
}


/*
*
*
*
*
*/


public function Rule_Int($var,$msg,$required){
if($required==true){
$Valid=(bool) ctype_digit($var);
if($Valid==false){
return $msg;
} 
}
if( (!empty($var) && $required==false)){
$Valid=(bool) ctype_digit($var);
if($Valid==false){
return $msg;
} 
}
}

/*
*
*
*
*
*/

public function Rule_String($var,$msg,$required){
if($required==true){
$Valid=(bool) ctype_alpha($var);
if($Valid==false){
return $msg;
} 
}
if( (!empty($var) && $required==false)){
$Valid=(bool) ctype_alpha($var);
if($Valid==false){
return $msg;
} 
}
}


/*
*
*
*
*
*/

public function Rule_Url($var,$msg,$required){
if($required==true){
$Valid=(bool) preg_match("@^(?:http://)?([^/]+)@i",$var);
if($Valid==false){
return $msg;
} 
}
if( (!empty($var) && $required==false)){
$Valid=(bool) preg_match("@^(?:http://)?([^/]+)@i",$var);
if($Valid==false){
return $msg;
}
}
}

/*
*
*
*
*
*/

public function Rule_Upload($var,$msg,$required){
if($required==true)
{
$Valid=(bool) preg_match("/^(.+)\\.(.+)/i",$var);
if($Valid==false){
return $msg;
} 
} 
} 


/*
*
*
*
*
*/

public function Rule_Extentions($var,$msg,$Mime,$required){
if($required==true){
$exe=end(explode('.',$var));
if(!in_array(strtoupper($exe),$Mime)){
return $msg;
}
}
if( (!empty($var) && $required==false)){
$exe=end(explode('.',$var));
if(!in_array(strtoupper($exe),$Mime)){
return $msg;
}
}
}
 














}













?>
Return current item: Validate form elements