PX : code

Power Form Validation by Zeljko
Download this code


top.html
-------------------------------------------------------------------------------
<html>
<head>

    <title>Form VAlidation</title>
</head>
<body >
<table  width=100% border=1 bgcolor="#C0C0C0">
<tr>
<th colspan=3 align="left" height="60">
<?php //print("Gore ");?>


</th>
</tr>
<tr>
<td  colspan=3 class="position" align="left" bgcolor="#E4E4E4" height=10>


</td>
</tr>
----------------------------------------------------------------------------
left.html
----------------------------------------------------------------------------
<tr>
    <td width=100%  colspan="3" ></td>
</tr>
<tr>
</td>
<td valign="top" align="center">
-----------------------------------------------------------------------------
bottom.html
-----------------------------------------------------------------------------
            </td>
            </td>
         </tr>
         <tr>
            <td  colspan=3 class="position" align="left" bgcolor="#E4E4E4" height=10></td>
         </tr>
         <tr>
            <th  colspan=3  height="50">
            </th>
         </tr>
      </table>
   </body>
</html>
--------------------------------------------------------------------------------
prvi.css
-----------------------------------------------------------------------------------
<STYLE>
            table { width:100%; margin: 0; padding: 0; font-size: small}
            table td{padding: 0; border-width: 0; vertical-allign: top; font-family: Verdana,Arial, Helvetica, sans-setif;}
            th {background-color: #2f5376; color: #FFFFFF; padding: 2px; vertical-align: middle; font-family: Verdana,Arial, Helvetica, sans-serif:}
           .position{font-size:70%;font-weight:normal;font-family: Verdana;color:#2f5376; };
           .outer{border: 1px solid silver;}
           .head {background-color: #c2cdd6; padding: 5px; font-weight: bold;}
           .head a {text-decoration: none; color:#2f5376; font-weight:bold;}
           .even {background-color: #dee3e7; padding: 5px;}
           .odd {background-color: #e9e9e9; padding: 5px;}
           .com{text-decoration: none; font-size:70%; background-color:#cccccc;}
</STYLE>
--------------------------------------------------------------------------------------
index.php
-----------------------------------------------------------------------------------------
<?php
include "print_report.php";
include 
"validation.php";
include 
"top.html";
include 
"left.html";
include 
"prvi.css";

$post $HTTP_POST_VARS;
$username=$post["username"];
$password=$post["password"];
$password1=$post["password1"];
$email=$post["email"];
$age=$post["age"];
$screen=$post["screen"];
$number=$post["number"];
$C1=$post["C1"];
$C2=$post["C2"];
$Industry=$post["Industry"];
$R1=$post["R1"];
$D1=$post["D1"];

if (isset(
$insert)) {
 
    
$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->set_operations("Industry"$Industry ,0);
    
$a->val_arr[0]=$C1;
    
$a->val_arr[1]=$C2;
    
$a->val_arr[2]=$C3;
    
$a->set_operations("Sex",$a->val_arr,6,1);
    
$a->set_operations("Yes/No",$R1,0);
    
$a->set_operations("Country",$D1,0);
    
//print($a-> print_operations());
    
if (count($a->errors)==0) {
       print(
"Form data's have successfully passed procedure of validation"); 
    }else{
        
$a->print_errors();
    }
    include 
"form.html";
     
}else{
    include 
"form.html";
}
include 
"bottom.html";


?>
-----------------------------------------------------------------------------------------
validation.php
-----------------------------------------------------------------------------------------
<?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
// ----------------------------------------------------------------------
// LICENSE
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; 
// This Class is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
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])==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])==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])==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])==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){
         
$tmpsplit("@",$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++){
            include 
"report1.html";
          }
        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++){
           include 
"report2.html";
          }
        print(
"</table>");
    }
}
?>
-------------------------------------------------------------------------------------------
report1.html
----------------------------------------------------------------------------------------
<tr>
<td   class="com"><?php print(" ---index:--- ".$i."----"); ?></td>
<td class="com" ><?php print($this->report_ar[$i]."</br>"); ?></td>
</tr>
------------------------------------------------------------------------------------------
report2.html
-------------------------------------------------------------------------------------------
<tr>
<td align="middle"  class="com" ><?php print($this->report_ar[$i]."</br>"); ?></td>
</tr>
---------------------------------------------------------------------------------------------
form.html
---------------------------------------------------------------------------------------------


    <FORM method="post" action="index.php?a=5">
        <TABLE border="1" bgcolor="#800000">
           <tr>
               <td width="20%" class="head">
                   Username: *
               </td>
               <td width="40%" class="even">
                   <INPUT type="text" name="username" value="<?php print($username); ?>" >
               </td>
               <td class="com">
                  
                   <b>$a->set_operations("Username",$username,0,3,5,">",5);</b><br/>
                   <ul>
                      <li>0:--Not Empty</li>
                      <li>3:--Has to be Letter</li>
                      <li>5,">",5:--Can not be longer than 5 Letters</li>
                   </ul>

               </td>
            </tr>
            <tr>
                <td class="head">
                    Password: *
                </td>
                <td class="even">
                    <INPUT type="password" name="password" value="<?php print($password); ?>" >
                </td>
                <td class="com">
                   
                    <b>$a->set_operations("Password",$password,0);</b><br/>
                     <ul>
                      <li>0:--Not Empty </li> 
                      
                   </ul>
                    <hr>
                    <b>$a->set_operations("Password and Again Password",$password,$password1,2);</b><br/>
                    <ul>
                      <li>2:--Has to be same like Again Password</li>
                      
                   </ul>
                </td>
           </tr>
           <tr>
               <td class="head">
                   Again Password: *
               </td>
               <td class="even">
                   <INPUT type="password" name="password1" value="<?php print($password1); ?>" >
               </td>
               <td class="com">
                  
                   <b>$a->set_operations("Again Password",$password1,0);</b> <br/>
                      <ul>
                      <li>0:--Not empty </li> 
                      
                   </ul>
                   <hr>
                   <b>$a->set_operations("Password and Again Password",$password,$password1,2);</b><br/>
                   <ul>
                      <li>2:--Has to be same like Again Password</li>
                      
                   </ul>
               </td>
           </tr>
           <tr>
               <td class="head">
                   Email: *
                </td>
                <td class="even">
                    <INPUT type="text" name="email" value="<?php print($email); ?>" >
                </td>
                <td class="com">
               
                    <b>$a->set_operations("E mail",$email,0,1);</b><br/>
                    <ul>
                      <li>0:--Not Empty</li>
                      <li>1:--E - mail has to be entered on appropriate way</li>
                      
                   </ul>
                </td>
           </tr>
           <tr>
               <td class="head">
                   Age: *
               </td>
               <td class="even">
                   <INPUT type="text" name="age" value="<?php print($age); ?>" >
               </td>
               <td class="com">
             
                   <b>$a->set_operations("age",$age ,0,4,5,">",2);</b><br/>
                   <ul>
                      <li>0:--Not Empty</li>
                      <li>4:--Just Numbers</li>
                      <li>5,">",2:--No Longer then 2 numbers</li> 
                      
                      
                   </ul>
               </td>
           </tr>
           <tr>
               <td class="head">
                   Screen Name: *
               </td>
               <td class="even">
                   <INPUT type="text" name="screen" value="<?php print($screen); ?>" >
               </td>
               <td class="com">
                   <b>$a->set_operations("Screen Name",$screen,0,3,5,"<",5);</b><br/>
                   <ul>
                      <li>0:--Not Empty</li>
                      <li>4:--Just Letters </li>
                      <li>5,"<",5:--Longer then 5 letters</li>
                      
                      
                   </ul>
               </td>
          </tr>
          <tr>
               <td class="head">
                   Some Number: 
               </td>
               <td class="even">
                   <INPUT type="text" name="number" value="<?php print($number); ?>" >
               </td>
               <td class="com">
                   <b>$a->set_operations("Some Number",$number,5,"=",10);</b><br/>
                   <ul>
                      <li>5,"=",10:--Can not be 10</li>
                      
                      
                      
                   </ul>
               </td>
          </tr>
          <tr>
              <td class="head">
                  Sex: *
              </td>
              <td class="even">
                  Man:
                  <input type="checkbox" name="C1" value="Man">
                  Woman:
                  <input type="checkbox" name="C2" value="Woman">
                  Not Defined:
                  <input type="checkbox" name="C3" value="NoD">
              </td>
              <td class="com">
              
                  <b>$a->val_arr[0]=$C1;</b><br/>
                  <b>$a->val_arr[1]=$C2;</b><br/>
                  <b>$a->val_arr[2]=$C3;</b><br/>
                  <ul>
                      <li>We are entering tree elements which we want to validate</li>
                  </ul>  
                  <b>$a->set_operations("Sex",$a->val_arr,6,1);</b><br/><br/>
                  <ul>
                      <li>6: One of this three elements has to be selected</li>
                      <li>1: Only one of this three elements can be selected</li>
                  </ul>
              </td>
          </tr>
          <tr>
              <td class="head">
                  Industry: *
              </td>
              <td class="even">
                  <select size="1" name="Industry">
                      <option value="">--Select--</option>
                      <option value="It">It</option>
                      <option value="Economics">Economics</option>
                  </select>
              </td>
              <td class="com">
                  <b>$a->set_operations("Industry", Industry ,0);></b><br/>
                  <ul>
                      <li>0:--Something has to be selected</li>
                  </ul>
              </td>
          </tr>
          <tr>
              <td class="head">
                  Yes/No: *
              </td>
               <td class="even">
                   Yes<input type="radio" value="V1" name="R1" >
                   No<input type="radio" value="V2"  name="R1" >
               </td>
               <td class="com">
                   <b>$a->set_operations("Yes/No",$R1,0);</b><br/>
                   <ul>
                       <li>0:--Something has to be selected</li>
                   </ul>
               </td>
           </tr>
           <tr>
               <td class="head">
                   Country: *
               </td>
               <td class="even">
                   <select size="3" name="D1">
                       <option value="aaa">Country1</option>
                       <option value="bbb">Country2</option>
                       <option value="ccc">Country3</option>
                       <option value="ddd">Country4</option>
                       <option value="eee">Country5</option>
                    </select>
                </td>
                <td class="com">
                    <b>$a->set_operations("Country",$D1,0);</b><br/>
                    <ul>
                        <li>0:--Something has to be selected</li>
                    </ul>
                </td>
            </tr>
            <tr>
                <td class="com" colspan="3">
                    <INPUT type="submit" value=insert  name=insert>
                </td>
            </tr>
        </TABLE>
    </FORM>
 ----------------------------------------------------------------------------------------------

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.