JavaScript
  Home arrow JavaScript arrow Page 4 - Adding Server-side Capabilities to Form Va...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Adding Server-side Capabilities to Form Validation with the DOM
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-11-07

    Table of Contents:
  • Adding Server-side Capabilities to Form Validation with the DOM
  • A quick look at the previous form validation script
  • Adding server-side capabilities to the original application
  • Completing the form validation class
  • Assembling the client and server-side modules of the application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Adding Server-side Capabilities to Form Validation with the DOM - Completing the form validation class


    (Page 4 of 5 )

    In consonance with the concepts discussed in the section that you just read, the next step that I’ll take now consists basically of adding some extra methods to the corresponding form validation class, which, as you know, was shown a few lines before.

    Having said that, below you can see the complete signature of this class. I've included some extra methods for validating specific data, like alphanumeric values and email addresses, and for counting errors as well. Take a look at the definition of the respective class, please:

    class FormValidator{     private $errors=array();     private $method;     const MIN=4;     const MAX=32;     public function __construct(){         $this->method=$_POST;     }     // validate empty values     public function validateEmpty($field,$errorMessage){         if(!$this->method[$field]||trim($this->
    method[$field])==''||strlen($this->method[$field])
    <self::MIN||strlen($this->method[$field])>self::MAX){             $this->errors[]=$errorMessage;         }     }     // validate integer values     public function validateInt($field,$errorMessage){         if(!$this->method[$field]||!is_numeric($this->
    method[$field])||intval($this->method[$field])!=$this->method[$field]){             $this->errors[]=$errorMessage;         }     }     // validate numeric values     public function validateNumber($field,$errorMessage){         if(!$this->method[$field]||!is_numeric($this->method[$field])){             $this->errors[]=$errorMessage;         }     }     // validate range of values     public function validateRange($field,$errorMessage){         if(!$this->method[$field]||$this->
    method[$field]<self::MIN||$this->method[$field]>self::MAX){             $this->errors[]=$errorMessage;         }     }     // validate alphanumeric values     public function validateAlphanum($field,$errorMessage){         if(!$this->method[$field]||!preg_match("/^[a-zA-Z0-9]+$/",
    $this->method[$field])){             $this->errors[]=$errorMessage;         }     }     // validate email address     public function validateEmail($field,$errorMessage){         if(!$this->method[$field]||!preg_match
    ("/.+@.+..+./",$this->method[$field])||!checkdnsrr
    (array_pop(explode("@",$this->method[$field])),"MX")){             $this->errors[]=$errorMessage;         }     }     // validate email address (Windows systems)     public function validateEmailWin($field,$errorMessage){         if(!$this->method[$field]||!preg_match(/^.+@.+..+$/,
    $this->method[$field])||!$this->windnsrr(array_pop(explode("@",
    $this->method[$field])),"MX")){             $this->errors[]=$errorMessage;         }     }     private function windnsrr($hostName,$recType=''){         if($hostName){             if($recType=='')$recType="MX";             exec("nslookup -type=$recType $hostName",$result);             foreach($result as $line){             if(preg_match("/^$hostName/",$line)){                 return true;             }             }             return false;         }         return false;     }     public function checkErrors(){         if(count($this->errors)>0){             return true;         }         return false;     }     public function displayErrors(){         $output='<ul>';         foreach($this->errors as $error){             $output.='<li>'.$error.'</li>';         }         $output.='</ul>';         return $output;     } }

     

     

    Okay, that's the complete signature of the form validation class.

    In this case, the only detail worth noticing is the addition of some brand new methods, useful for checking specific data, as well as the definition of “checkErrors()” and “countErrors(),” handy for counting and displaying errors respectively.

    At this point, and assuming that the form checking class that you saw before is now a clear concept in your mind, the final step that I plan to take simply consists of putting the DOM-based validation script created previously to work together with the PHP class in question. In this way, you’ll be able to have at your disposal a fully-featured form validation system, which not only checks for invalid data in the client, but it performs the corresponding validation process in the server too.

    To see how these two modules of the application work in conjunction, please read the final section of this article. We’re almost finished!

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this final part of the series, the original form validation application is...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT