HTML
  Home arrow HTML arrow Page 3 - DHTML Form Enhancement
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  
Moblin 
JMSL Numerical Library 
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? 
HTML

DHTML Form Enhancement
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 36
    2004-06-21

    Table of Contents:
  • DHTML Form Enhancement
  • Visual Appeal
  • Extensible Validation
  • Regex Validation and Equality Checking
  • Extending the validate function

  • 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


    DHTML Form Enhancement - Extensible Validation


    (Page 3 of 5 )

    Consider the following DHTML:

     function isNotEmpty(elem) {
      var str = elem.value;
      if(str == null || str.length == 0)
       return false;
      else
       return true;
     }
     
     // THIS FUNCTION PERFORMS VALIDATION BASED ON A SET OF CUSTOM HTML ATTRIBUTES
     function validate(form) {
      var attrVal, attrReg, attrEq, attrFail, strTemp;

      for (var i = 0; i < form.length; i++) {
       attrVal = form[i].getAttribute("validate");
       
       switch (attrVal) {
        case 'required' :
        if (!isNotEmpty(form[i])) {
         attrFail = form[i].getAttribute("failure");
         
         if (attrFail)
          alert(attrFail);
         else     
          alert('You must complete all required form fields.');
         form[i].focus();
         return false;
        }
        break;
        
        case 'regex' :
        attrReg = form[i].getAttribute("regex");
        if (attrReg != null && attrReg.length != 0) {
         var regex = new RegExp(attrReg);
         strTemp = form[i].value;
         if (!strTemp.match(regex)) {
          attrFail = form[i].getAttribute("failure");

          if (attrFail)
           alert(attrFail);
          else
           alert('Invalid data format at field "' + form[i].name + '".');
          form[i].focus();
          return false;
         }
        }
        break;
        
        case 'equals' :
        attrEq = form[i].getAttribute("equals");
        var objEq = document.getElementById(attrEq);
        if (objEq) {
         if (form[i].value != objEq.value) {
          attrFail = form[i].getAttribute("failure");
          
          if (attrFail)
           alert(attrFail);
          else
           alert('Form fields do not match');
          
          form[i].focus();
          return false;
         }
        }
        break;
       }
       
      }
      
      return true;
     }

    Examine the function above; the most notable part of the function is the switch statement that tests the value of the element's 'validate" attribute. The function supports required field validation, field equality validation, and regular expression validation. The isNotEmpty function is just a wrapper function to make sure a field is not empty. To enable the form to use validation, we will use the following code for the form's onsubmit handler:

    <form onsubmit="return validate(this);" ></form>

    The code in the validate function provides functionality to validate information in form fields. Using this DHTML, to make a form field required, we would use the following markup:

    <input validate="required" />

    Obviously you would have more attributes but this example shows how to use the DHTML above to make a form field required. The next page will discuss slightly more complex uses of the function.

    More HTML Articles
    More By David Fells


     

    HTML ARTICLES

    - Completing Construction of a Database Form w...
    - Maximizing and Restoring Images in a Tabular...
    - Building the Recordset for an HTML Database ...
    - Laying Out a Database Form with HTML
    - Tabular Database Form Functions with HTML
    - Tabular Database Forms with HTML
    - Using the Find Functions for HTML Database F...
    - Sorting for Database Forms with HTML
    - Edit and Other Database Form Functions with ...
    - More Database Form Functions with HTML
    - Database Form Functions with HTML
    - Using the HTML Table Element as a Recordset
    - Building Single Row Database Forms with HTML
    - Introduction to Database Forms with HTML
    - Another Look at Animation of Geographical Ma...






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