iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 13, 2008
Rainbow Links
EarthWeb.com
 
May 13, 2008
MySpace Profile Page Resources
HTML Goodies
 
May 13, 2008
How to Upload Your Photos onto the Web
HTML Goodies
 
May 13, 2008
Email Marketing for MySpace Artists
HTML Goodies
 
May 13, 2008
Top Online Marketing Techniques
HTML Goodies
 
May 13, 2008
I want to create a site just like ____, is that a violation of...
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /JavaScript

How Not to Script HTML Form Validation 

  Views:    55738
  Votes:    9
by George Jempty 1/23/05 Rating: 

Synopsis:

Avoid some common errors when using Javascript to perform client-side HTML form validation
Pages: firstback1 forwardlast
The Article

Following the colorful parlance for AntiPatterns, the erroneous example on page 1 might be called the “Out of Sight Out of Mind” AntiPattern. Except that AntiPatterns typically describe a problem that is more systemic, rather than one that can be so readily rectified by correcting one line of code. For instance, failure -- or refusal -- to use a language feature such as Regular Expressions.

Better yet exemplifying a bona fide AntiPattern in a Javascript form validation context is what I call the “All Web Development is New Development” fallacy. This antiPattern, like the code defect debunked in the first example, is commonplace both in books and in code libraries distributed all over the web.

“All Web Development is New Development” in the Javascript form validation context most often occurs through the suggestion that, to signify that form fields are required to be filled in, these required fields must be named in a certain manner. Users of such code are instructed to do such things as end the names of their form fields with
“_req” .

So, in our first example,
“fname” would become “fname_req” , and “lname” would become “lname_req” . To begin with, though, this violates one of Arthur J Riel's Object-Oriented Design Heuristics, particularly 3.5, which states in part: “In applications which consist of an object-oriented model interacting with a user interface, the model should never be dependent on the interface.”

The HTML comprises the user interface. Now substitute “data” for “model” above. Data is required to specify one field as required, and another as not required. But by appending “_req” to the name of an HTML form element, the data becomes dependent on the interface.

Rules are made to be broken, but not in this instance. Because, although requiring that form field names have “_req” be appended to them might work in the case of a new web application, what about an existing web application? It almost certainly consists in part of server-side form processing code that relies on pre-determined form field names. Adding “_req” to those field names will almost certainly cause existing server-side code to malfunction.

So, not all web development is new development. And the above examples are certainly not how any enterprise web form validation should be undertaken. For an example how to better perform HTML form validation with Javascript, please check devpapers.com later this year for upcoming articles, or my own website (see my devpaper's author profile) for an upcoming book.

Pages: firstback1 forwardlast

Similar/related articles:


 
  Sponsors