JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - A quick look at the previous form validation script


    (Page 2 of 5 )

    As usual, first I’d like to show you the complete source code that corresponds to the form validation script that I developed in the previous article, before we make any attempt to code a PHP class that checks user-supplied data on the server. This preceding stage will help you understand more easily how the respective client and server-side modules that make up the application fit together.

    Here is the complete definition for the form validation script created in the previous tutorial. Please take a look at the code listing below:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>DOM-based form validator</title> <script language="javascript"> // validate form function validateForm(formObj){     valid=true;     var fname=formObj.elements[0];     if(!fname){return};     if(!fname.value){showError(fname,'Enter your First Name')};     var lname=formObj.elements[1];     if(!lname){return};     if(!lname.value){showError(lname,'Enter your Last Name')};     var email=formObj.elements[2];     if(!email){return};     if(!email.value){showError(email,'Enter your email address')};     var age=formObj.elements[3];     if(!age){return};     if(!age.value){showError(age,'Enter your age (1-99)')};     var postadd=formObj.elements[4];     if(!postadd){return};     if(!postadd.value){showError(postadd,'Enter your postal address')};     return valid; } // display error messages function showError(obj,message){     if(!obj.errorNode){         obj.onchange=hideError;         var span=document.createElement('span');         span.className='error';         span.appendChild(document.createTextNode(message));         obj.parentNode.appendChild(span);         obj.errorNode=span;     }     valid=false;     return } // hide error messages function hideError(){     this.parentNode.removeChild(this.errorNode);     this.errorNode=null;     this.onchange=null; } // execute 'ValidateForm()' function when page is loaded window.onload=function(){     // check if browser is W3CDOM compatible     if(document.getElementById&&document.
    getElementsByTagName&&document.createElement){             var theform=document.getElementsByTagName('form')[0];             if(theform){theform.onsubmit=function(){return
    validateForm(this)}};     } } </script> <style type="text/css"> body{     margin: 0;     padding: 0;     background:#fff; } h1{     font: bold 18px Arial, Helvetica, sans-serif;     color: #000;     text-align: center; } span{     font: bold 12px Arial, Helvetica, sans-serif;     color: #f00;     margin-left: 10px; } ul{     font: bold 12px Arial, Helvetica, sans-serif;     color: #f00;     text-align:center; } #formcontainer{     width: 600px;     height: 450px;     background:#eee;     margin-left:auto;     margin-right:auto;     border: 1px solid #ccc; } #labelcontainer{     float: left;     width: 200px;     text-align: right;     padding-right: 10px; } #labelcontainer p{     font: normal 12px Arial, Helvetica, sans-serif;     color: #000;     margin: 10px 0 0 0; } #boxcontainer{     float: left;     width: 300px; } #boxcontainer p{     font: normal 12px Arial, Helvetica, sans-serif;     color: #000;     margin: 4px 0 0 0; } </style> </head> <body> <div id="formcontainer"> <form action="<?php echo $_SERVER['PHP_SELF']?>"method="post"> <div id="labelcontainer"> <p>First Name</p> <p>Last Name</p> <p>Email</p> <p>Age</p> <p>Postal Address</p> <p>Comments</p> </div> <div id="boxcontainer"> <p><input type="text" name="fname" /></p> <p><input type="text" name="lname" /></p> <p><input type="text" name="email" /></p> <p><input type="text" name="age" /></p> <p><input type="text" name="paddress" /></p> <p><textarea rows="10" cols="20"></textarea></p> <p><input type="submit" name="send" value="Send Data" /></p> </div> </form> </div> </body> </html>

    As you’ll recall, the above source code corresponds to the DOM-based form validation system created in the prior part of the series. Naturally it’s limited to performing client-side validation on the data entered in the respective sample contact form. From this point onward, you can implement the application with its current capabilities, or…you can be more ambitious and learn how to integrate the system with strong data checking functionality provided by a server-side scripting language such as PHP.

    Did you decide to take the last approach? Good! Therefore, move on and read the following section, which will be entirely focused on defining an efficient PHP class that neatly complements the previous client-side form validation script.  

    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 5 hosted by Hostway
    Stay green...Green IT