WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Power Form Validation
Categories : PHP, PHP Classes, Data Validation Click here to Update Your Picture
Zeljko Radulovic
Date : Nov 09th 2005
Grade : 3 of 5 (graded 4 times)
Viewed : 7068
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Zeljko Radulovic
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php
// File:
// Name: Validation.php
// Date: 2005/11/03 06:30:18
// ----------------------------------------------------------------------
//
// Power Form Validation
// Author: Zeljko Radulovic
// http://pad.pmf.kg.ac.yu/articles/index.php?action=author
// ----------------------------------------------------------------------
// Documentation:http://pad.pmf.kg.ac.yu/am/index.php?action=preg_kursa&co_id=7
// Download:http://pad.pmf.kg.ac.yu/am/index.php?action=preg_kursa&co_id=7
// ----------------------------------------------------------------------
class validation {

    var
$val_components    =array();
    var
$val_operations    =array();
    var
$val_arr        =array();
    var
$errors            =array();
    var
$traker            =array();
    var
$name_traker       =array();
    var
$counter;
    var
$exit_par;
    var
$a;           
   
    function
validation(){
   
       
$this->val_operations[0]="Controls if is field Empty or if is component selected";
       
       
$this->val_operations[1]="Controls with help of Regular Expressions if is e-mail "
                               
." address entered on right way ";
                               
       
$this->val_operations[2]="Controls if two of entered components have same value";
       
       
$this->val_operations[3]="Controls if is entered value in text box in form of letters";
       
       
$this->val_operations[4]="Controls if is entered value in text box in form of numbers";
       
       
$this->val_operations[5]="lenght";
       
       
$this->val_operations[6]="Controls how many components are not selected or in how many "
                                 
." of them value is not entered (in case of text field) and "
                                 
." compares that number with given value ";
       
$this->counter=0
    }
    function
print_operations(){
       
$a= new print_report($this->val_operations);
       
$a-> make_report1();
    }
    function
set_operations(){
       
$this->exit_par=false;
       
$numargs = func_num_args();
        for (
$i=2;$i<$numargs;$i++){
            switch (
func_get_arg($i))
                { 
                case
"0":
                   
$name=func_get_arg(0);
                   
$value=func_get_arg(1);
                   
$this->empty_error($name,$value);
                   break;
                case
"1":
                    if ((
$this->name_traker[$this->counter-1])==func_get_arg(0) and ($this->traker[$this->counter-1])==1 and (func_get_arg(2)==0)) {
                        break;
                    }else{
                       
$name=func_get_arg(0);
                       
$value=func_get_arg(1);
                       
$this->email_error($name,$value);
                        break;
                    }
                case
"2":
                   
$name=func_get_arg(0);
                   
$value1=func_get_arg(1);
                   
$value2=func_get_arg(2);
                    if (
trim($value1)=='' or trim($value2)=='') {
                        break;
                    }elseif(
func_get_arg(2)==2){
                        break;
                    }else{
                       
$name=func_get_arg(0);
                       
$value1=func_get_arg(1);
                       
$value2=func_get_arg(2);
                       
$this->same_error($name,$value1,$value2);
                        break;
                    }
                case
"3":
                    if ((
$this->name_traker[$this->counter-1])==func_get_arg(0) and ($this->traker[$this->counter-1])==1 and (func_get_arg(2)==0)) {
                         break;
                    }else{
                       
$name=func_get_arg(0);
                       
$value=func_get_arg(1);
                       
$this->string_error($name,$value);
                        break;
                    }
                case
"4":
                    if ((
$this->name_traker[$this->counter-1])==func_get_arg(0) and ($this->traker[$this->counter-1])==1 and (func_get_arg(2)==0)) {
                        break;
                    }else{
                       
$name=func_get_arg(0);
                       
$value=func_get_arg(1);
                       
$this->int_error($name,$value);
                        break;
                    }
                case
"5":
                    if ((
$this->name_traker[$this->counter-1])==func_get_arg(0) and ($this->traker[$this->counter-1])==1 and (func_get_arg(2)==0)) {
                        break;
                    }else{
                       
$name=func_get_arg(0);
                       
$value=func_get_arg(1);
                        unset(
$tr);
                       
$tr=func_get_arg($i+1);
                       
$lang=func_get_arg($i+2);
                       
$this->lang_error($name,$value,$tr,$lang);
                       
$this->exit_par=true;
                        break;
                   }
                   case
"6":
                       
$name=func_get_arg(0);
                       
$value=func_get_arg(1);
                       
$num=func_get_arg(3);
                       
$this->arr_error($name,$value,$num);
                        break;
                   
            }
            if (
$this->exit_par===true) {
               break; 
            } 
        }
    }
    function
arr_error($name,$value1,$num){
       
$n=count($value1);
       
       
$co=0;
        for (
$i=0;$i<$n;$i++){
            if (
$value1[$i]=='') {
               
$co=$co+1;
            }
        }
       
        if (
$co==$n) {
           
$this->errors[]="You didn't selected value for field: <b>" .$name. "</b> </br>";
           
$this->traker[]=1;
           
$this->name_traker[]=$name;
           
$this->counter=$this->counter+1;
           
$this->exit_par=true;
            return
$this->traker;   
        }else
        if (
$co < ($n-$num)) {
           
$this->errors[]="You should selected just ".$num." value for field: <b>" .$name. "</b> </br>";
           
$this->traker[]=1;
           
$this->name_traker[]=$name;
           
$this->counter=$this->counter+1;
           
$this->exit_par=true;
            return
$this->traker;
           
        }else{
       
       
$this->exit_par=true;
        }
       
    }
    function
lang_error($name,$value1,$op,$lang){
       
$l=strlen($value1);
        switch (
$op)
            {
            case
"<":
                if (
$l < $lang) {
                   
$this->errors[]="You didn't enter appropriate value for field: <b>" .$name." </b>  this field can not be shorter than ".$lang."</br>";
                   
$this->traker[]=1;
                   
$this->name_traker[]=$name;
                   
$this->counter=$this->counter+1;
                    return
$this->traker;
                    break;
                }else{
                    break;
                }
            case
">":
                if (
$l > $lang) {
                   
$this->errors[]="You didn't enter appropriate value for field: <b>" .$name. "</b> this field can not be longer than ". " " .$lang."</br>";
                   
$this->traker[]=1;
                   
$this->name_traker[]=$name;
                   
$this->counter=$this->counter+1;
                    break;
                 }else{
                     break;
                 }
             case
"=":
                 if (
$value1 == $lang) {
                     
$this->errors[]="You didn't enter appropriate value for field: <b>" .$name. "</b> this field can not be ".$lang."</br>";
                     
$this->traker[]=1;
                     
$this->name_traker[]=$name;
                     
$this->counter=$this->counter+1;
                     break;
                 }else{
                     break;
                 }
        }
    }
    function
int_error($name,$value1){
        if (
eregi("[a-zA-Z/<\/\#$@'$=w+-?>./]",$value1)) {
           
$this->errors[]="You didn't enter appropriate value for field: <b>" .$name. "</b> this field has to be Number</br>";
           
$this->traker[]=1;
           
$this->name_traker[]=$name;
           
$this->counter=$this->counter+1;
           
$this->exit_par=true;
            return
$this->traker;
        }
    }
    function
string_error($name,$value1){
        if (
eregi("[0-9/<\/\'$=w+-?>./]",trim($value1))) {
           
$this->errors[]="You didn't enter appropriate value for field: <b>" .$name. "</b> this field has to be Letters</br>";
           
$this->traker[]=1;
           
$this->name_traker[]=$name;
           
$this->counter=$this->counter+1;
           
$this->exit_par=true;
            return
$this->traker;
        }
    }
    function
empty_error($name,$value){
        if (
trim($value)=='') {
           
$this->errors[]="You didn't enter value for field: <b>" .$name. "<b/></br>";
           
$this->traker[]=1;
           
$this->name_traker[]=$name;
           
$this->counter=$this->counter+1;
           
$this->exit_par=true;
            return
$this->traker;
         }
     }
     function
same_error($name,$value1,$value2){
         if (
trim($value1)!=trim($value2)) {
             
$this->errors[]="You didn't enter same value for fields <b>" .$name. "<b/></br>";
             
$this->traker[]=1;
             
$this->name_traker[]=$name;
             
$this->counter=$this->counter+1;
             
$this->exit_par=true;
             return
$this->traker;   
          }
     }
     function
is_hostname ($host){
         if (!
eregi("^[a-z0-9]{1}[a-z0-9\.\-]*\.[a-z]{2,}$",$host ))
             return
false;
         if (
ereg("\.\.",$host)||ereg("\.-",$host)||ereg ("-\.",$host))
             return
false;
         return
true;         
     }
     function
is_email($email){
         
$tmp= split("@",$email);
         if (
count($tmp)<1)
            return
false;
         if (
count($tmp)>2)
            return
false;
         @
$username = $tmp[0];
         @
$hostname = $tmp[1];
         if (!
eregi("^[a-z0-9_\+\.\-]+$",$username ))
            return
false;
         if(!
$this->is_hostname ($hostname))
            return
false;
         return
true;
     }
     function
email_error($name,$value1){
        if (!
$this->is_email($value1)){
             
$this->errors[]="You didn't enter e-mail on right way  <b>" .$name. "<b/></br>";
             
$this->traker[]=1;
             
$this->name_traker[]=$name;
             
$this->counter=$this->counter+1;
             
$this->exit_par=true;
              return
$this->traker;
        }
     }   
     function
print_errors(){
         
$a= new print_report($this->errors);
         
$a-> make_report2();
     }
}
?>



print_report.php
<?php
class print_report {

    var
$report_ar        =array();
   
    function
print_report($par){
       
$this->report_ar=$par;
    }
    function
make_report1(){
       
$n=count($this->report_ar);
        print(
"<table width=100 border=1><tr><th colspan=2>Operations</th></tr>");
        for (
$i=0;$i<$n;$i++){
            print(
"<tr><td> ---index:---".$i."----</td>");
            print(
"<td >".$this->report_ar[$i]."</br></td></tr>");
          }
        print(
"</table></br>");
    }
    function
make_report2(){
       
$n=count($this->report_ar);
        print(
"<table border=1><tr><th colspan=2>Validation Report</th></tr>");
        for (
$i=0;$i<$n;$i++){
            print(
"<tr><td align=middle >".$this->report_ar[$i]."</br></td></tr>");
        }
        print(
"</table>");
    }
}
?>



index.php
<?php
include "print_report.php";
include
"validation.php";
/*
Here is 4 examples of validating form inputs - only
what you have to do is to let different examples to work.
Complete documentation for this class and one Example you cam find:
http://pad.pmf.kg.ac.yu/am/index.php?action=preg_kursa&co_id=7
*/

/*   EXAMPLE1
*
*
    $username="";
    $password="";
    $password1="";
    $email="";
    $age="";
    $screen="";
    $number="";
    $C1="";
    $C2="";
    $C3="";
*/   
   

/*  EXAMPLE2
*
*
    $username="Yeljko1";
    $password="Peric";
    $password1="";
    $email="dfgdfgf";
    $age="45y";
    $screen="3456";
    $number="10";
    $C1="a";
    $C2="b";
    $C3="";
*/ 
   

/*  EXAMPLE3
*
*
    $username="Yeljkooo";
    $password="Peric";
    $password1="Perici";
    $email="ryeljko2004@yahoo.com";
    $age="456";
    $screen="rrr";
    $number="6";
    $C1="a";
    $C2="b";
    $C3="c";
*/   


/*  EXAMPLE4
*
*
    $username="Yelj";
    $password="Peric";
    $password1="Peric";
    $email="ryeljko2004@yahoo.com";
    $age="45";
    $screen="rrrrrrr";
    $number="6r";
    $C1="a";
    $C2="";
    $C3="";
*/   
     

   
$a =new validation();
   
$a->set_operations("Username",$username,0,3,5,">",5);
   
$a->set_operations("Password",$password,0);
   
$a->set_operations("Again Password",$password1,0);
   
$a->set_operations("Password and Again Password",$password,$password1,2);
   
$a->set_operations("E mail",$email,0,1);
   
$a->set_operations("age",$age ,0,4,5,">",2);
   
$a->set_operations("Screen Name",$screen,0,3,5,"<",5);
   
$a->set_operations("Some Number",$number,5,"=",10);
   
   
$a->val_arr[0]=$C1;
   
$a->val_arr[1]=$C2;
   
$a->val_arr[2]=$C3;
   
$a->set_operations("Sex",$a->val_arr,6,1);
   
   if (
count($a->errors)==0) {
       print(
"Form data's have successfully passed procedure of validation");
    }else{
       
$a->print_errors();
    }
?>



Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides
Validator - A PHP class that can can be used for validating Email IDs and Dates
Categories : PHP, PHP Classes, Data Validation, Email, Date Time
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself.
Categories : PHP, PHP Classes, Templates, Complete Programs
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)
Password Creator: This PHP code exmaple shows how to use bitwise operations on a single variable and using it as a flagged variable. The class generates passwords of a given length using specified characters and the flags.
Categories : PHP, PHP Classes, Algorithms, Security
an example of the cyberlib payment class
Categories : PHP, PHP Classes, Ecommerce, Credit Cards
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
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