Location: PHPKode > scripts > Validation Class > VALIDATOR.class_v1.php
<?php

/*
	A Peace and Love Handwriting
	Author: OJUTIKU OLUWATOBI
*/

abstract class validator_v1{
	
	private function __construct(){}
	private function __clone(){}	
	
	private static function vemail($em) {
		return eregi("^[a-z0-9._-]+@[a-z0-9._-]+.[a-z]{2,6}$", $em) ? true : false;
	}
	
	private static function vnum($num) {
		return is_numeric($num) ? true : false;
	}
	
	private static function vstr($str) {
		return ctype_alpha($str) ? true : false;
	}
	
	private static function vstrnum($ch) {
		return ctype_alnum($ch) ? true : false;
	}
	
	private static function vsp_strnum($ch, $allowed = array()) {
		
		if( !is_array( $allowed ) ) {
			$allowed = explode(',', $allowed);
		}
		
		//a bug occurs if range begins from 0, 9
		$allowed_list = array_merge(range(a, z), range(1, 9), $allowed);
		$allowed_list = array_unique($allowed_list);
		
		$ch = strtolower( $ch );
		for($i=0, $k=strlen($ch); $i < $k; ++$i ) {
			if( !in_array($ch[$i], $allowed_list) ) {
				return false;
			}
		}
			return true;
			
	}
	
	private static function verrname( $errt ) {
		$return = '';
		switch( $errt ) {
			case 'em':
				$return = 'Invalid Email';
			break;
			case 'num':
				$return = 'Numbers Only';
			break;
			case 'alpnum':
				$return = 'Numbers and Alphabets Only';
			break;
			case 'alp':
				$return = 'Alphabets Only';
			break;
			case 'sp_alpnum':
				$return = 'Unknown Character Found';
			break;
			case 'emp':
				$return = 'Émpty';
			break;		
		}
		return $return;
	}
	
	

	public function s_valid( $vd, $ty, $sp = false ){
		$return = '';
		switch( $ty ){
			case 'em':
				$return = self::vemail($vd) ? 0 : 1;
			break;
			case 'num':
				$return = self::vnum($vd) ? 0 : 1;
			break;
			case 'alpnum':
				$return = self::vstrnum($vd) ? 0 : 1;
			break;
			case 'alp':
				$return = self::vstr($vd) ? 0 : 1;
			break;
			case 'sp_alpnum':
				$return = self::vsp_strnum($vd, $sp) ? 0 : 1;
			break;
			case 'emp':
				$return = !empty($vd) ? 0 : 1;
			break;
		}
		return $return;
	}
	
	
	
	public function m_valid( $valid = array() ){
		
		$return = '';
		foreach( $valid as $v ) {
			if( $return == 1 ) {
				break;
			}
			switch( $v['typ'] ){
				case 'em':
					$return = self::vemail($v['v']) ? 0 : 1;
				break;
				case 'num':
					$return = self::vnum($v['v']) ? 0 : 1;
				break;
				case 'alpnum':
					$return = self::vstrnum($v['v']) ? 0 : 1;
				break;
				case 'alp':
					$return = self::vstr($v['v']) ? 0 : 1;
				break;
				case 'sp_alpnum':
					$return = self::vsp_strnum($v['v'], $v['sp']) ? 0 : 1;
				break;
				case 'emp':
					$return = !empty($v['v']) ? 0 : 1;
				break;				
			}
		}
		return $return;
		
	}
	
	

	
	
	public function xm_valid( $valid = array() ){
		
		$returns = array();
		$return = '';
		foreach( $valid as $v ) {

			switch( $v['typ'] ){
				case 'em':
					$return = self::vemail($v['v']) ? 0 : 1;
				break;
				case 'num':
					$return = self::vnum($v['v']) ? 0 : 1;
				break;
				case 'alpnum':
					$return = self::vstrnum($v['v']) ? 0 : 1;
				break;
				case 'alp':
					$return = self::vstr($v['v']) ? 0 : 1;
				break;
				case 'sp_alpnum':
					$return = self::vsp_strnum($v['v'], $v['sp']) ? 0 : 1;
				break;
				case 'emp':
					$return = !empty($v['v']) ? 0 : 1;
				break;				
			}
			if( $return == 1 ) {
				$returns[] = array('name'=>$v['name'], 'errtype'=>self::verrname($v['typ']) );
				$return = '';
			}
			
		}
		return $returns;
		
	}


}


/*

#USAGE MODE
for single validation
use validator_v1::s_valid( $vd, $ty, $sp = false );
where
$vd -> represents the data to validate
$ty -> represents the validation type to check
$sp -> represents the characters to check [OPTIONAL]


for multiple validation
use validator_v1::m_valid( 
	array(
		array( 'v'=>$vd, 'typ'=>$typ, 'sp'=>$sp ),
		array( 'v'=>$vd, 'typ'=>$typ, 'sp'=>$sp ),
		array( 'v'=>$vd, 'typ'=>$typ, 'sp'=>$sp )
	) 
);


for multiple validation with error spot
use validator_v1::xm_valid( 
	array(
		array( 'name'=>'', 'v'=>$vd, 'typ'=>$typ, 'sp'=>$sp ),
		array( 'name'=>'', 'v'=>$vd, 'typ'=>$typ, 'sp'=>$sp ),
		array( 'name'=>'', 'v'=>$vd, 'typ'=>$typ, 'sp'=>$sp )
	) 
);


*/

?>
Return current item: Validation Class