PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Mark Mendoza  >  PHP Form Data Validation  >  index.php  >  Download  
File: index.php
Role: Class source
Content type: text/plain
Description: PHP Data Form Validation
Class: PHP Form Data Validation
Validate arrays of data using a list of rules
Author: By
Last change: -Added new rules: match and regex
public static function regex($val,$regex){
return preg_match($regex,$val);
}

public static function match($val,$param){
return $val == $param;
}

-changed $error property as non static
Date: 4 years ago
Size: 3,309 bytes
 

Contents

Class file image Download
<?php
/*
 * Form Data validation
 *
 *
 * Full example:
 *
 * $rules = array(
 * 'username' => 'charlength:8-16,alphanumeric',
 * 'alphanumeric' => 'alphanumeric',
 * 'required' => 'required',
 * 'email' => 'email',
 * 'alpha' => 'alpha',
 * 'password' => 'password',
 * 'range' => 'range:1-16',
 * 'match' => 'match:this is a test',
 * 'regex' => 'regex:/quick/'
 * );
 *
 * $data = array(
 * 'username' => 'markmendoza123',
 * 'alphanumeric' => 'aaaaaa111111',
 * 'required' => '',
 * 'email' => 'markbmendoza@gmail.com',
 * 'alpha' => 'aaaaaabbbbbbccccc',
 * 'password' => '6alphanumeric',
 * 'range' => '8',
 * 'match' => 'this is a test',
 * 'regex' => 'the quick brown'
 *
 * );
 *
 * $v = new \validation\validate($data, $rules);
 *
 * if(!$v->success()){
 * var_dump($v->$errors);
 * }else{
 * echo 'Success.';
 * }
 *
 *
 */
namespace validation{

    class
rules{
       
/* Add your rules here, return true means valid */

       
public static function regex($val,$regex){
            return
preg_match($regex,$val);
        }

        public static function
match($val,$param){
            return
$val == $param;
        }

        public static function
required($val){
            return !empty(
$val);
        }

        public static function
charlength($val,$param){
           
$l = strlen($val);
           
$p = explode('-',$param);

            return
$l >= $p[0] && $l <= $p[1];
        }

        public static function
alphanumeric($val){
            return
preg_match('/^([0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+)[0-9a-zA-Z]*$/', $val);
        }

        public static function
email($val){
            return
preg_match('/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/',$val);
        }
       
        public static function
alpha($val){
            return
preg_match('/^[A-Za-z]+$/',$val);
        }
       
        public static function
password($val){
            return
preg_match('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/',$val);
        }
       
        public static function
url($val){
            return
preg_match('/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/',$val);
        }
       
        public static function
range($val,$param){
          
           
$p = explode('-',$param);

            return
$val >= $p[0] && $val <= $p[1];
        }
       

    }

    class
validate{
        public
$errors = false;
        public function
__construct($data, $rules){

           
//array('username' =>'charlength:8-16')
           
foreach($rules as $key => $val){
               
//charlength:8-16,alphanumeric
               
$rule = explode(',',$val);
                foreach(
$rule as $k => $v){
                   
//charlength:8-16
                   
$p = explode(':',$v,2);
                    if( !isset(
$data[$key]) || (isset($data[$key]) && !rules::{$p[0]}($data[$key],isset($p[1])?$p[1]:null) ) ){
                       
$this->$errors[$key] = $val;
                    }
                }
            }
        }

        public function
success(){
            return !
$this->$errors;
        }

    }
   



}