Php validation class

Client side validation is done with javascript , but for robust application we should perform sever side validation as well. It makes php application more secure and reliable. Here a php validation class is given with static methods. You can include in your project for performing form data validation. Almost useful functions have been defined in this class. According to your need you may add more.


<?php
// Validation.php
class Validation
{
		public static function isEmpty($val)
		{
				$val=trim($val);
				return empty($val);
		}
		
		public static function isEmail($val)
		{	$val=trim($val);
		  
		  	return (bool)preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $val);
		}
		
		
		
		public static function isDigits($val)
		{
				$val=trim($val);
				return (bool)preg_match( '/^[0-9]+$/', $val);
		}
		
		public static function isInteger($val)
    	{
        	return (bool)preg_match( '/^[\-+]?[0-9]+$/', $val);
    	}
		
    	public static function isNumber($val)
    	{
        	return (bool)preg_match( '/^[0-9]+\.?[0-9]+$/', $val);
    	}
		
    	public static function isMaxLength($str,$val)
    	{
        	if(strlen($str)<=$val)
            	return TRUE;
        	return FALSE;
    	}
    	
		public static function isMinLength($str,$val)
    	{
        	if(strlen($str)>=$val)
            return TRUE;
        	return FALSE;
    	}
	
		public static function isEqualLength($str,$val)
    	{	
        	if(strlen($str)==$val)
            return TRUE;
        	return FALSE;
    	}
		
		public static function isEqualTo($str,$val)
    	{
        	if($str===$val)
            return TRUE;
        	return FALSE;
    	}
}

How to use: Php validation class

Required Field validation

<?php
require_once("Validation.php");
$val1="";

if(Validation::isEmpty($val1))
	echo "Value is empty. This field is required";
 else
 	echo "value is not empty";

Email validation

//
<?php
require_once("Validation.php");
$email="abc@dkmoppp";

if(!Validation::isEmpty($email))
	echo "Email address is not valid";
 else
 	echo "Email address is valid";

Digits validation

//
<?php
require_once("Validation.php");
$val="123";

if(Validation::isDigits($val))
	echo "valid";
$val="1232.612"
if(Validation::isDigits($val))
 	echo "valid";
 else
 	echo "Invalid";

Integer validation

//
<?php
require_once("Validation.php");
$val="-123";

if(Validation::isInteger($val))
	echo "valid";
$val="1232.612"
if(Validation::isInteger($val))
 	echo "valid";
 else
 	echo "Invalid";