JavaScript
  Home arrow JavaScript arrow Form Validation with JavaScript Regular Ex...
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

Form Validation with JavaScript Regular Expressions (Part 2)
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 89
    2005-05-17

    Table of Contents:
  • Form Validation with JavaScript Regular Expressions (Part 2)
  • User-friendly enhancement
  • Defining the regular expressions
  • Testing the values submitted

  • 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


    Form Validation with JavaScript Regular Expressions (Part 2)


    (Page 1 of 4 )

    Form validation is an issue many websites must deal with. In this second article in a two-part series, you will use the JavaScript regular expressions you learned in the first article to put together a script that will validate a range of form inputs.

    In the first part of this article, I explained some of the basic building blocks of JavaScript regular expressions, and mentioned that a lot can be done with them to assist in validating the information received through online forms. In this part, I will cover what can be done with these building blocks in more detail.

    Using the examples from part one (and building upon them), you can quickly put together a script that will validate a range of inputs. I’ve used the following HTML form as an example. It’s not the greatest of forms, but it serves to illustrate this example:

    <html>
    <head>
    <title>My Form</title>
    </head>
    <body>
    <h1>Sign-up For Our Newsletter</h1>
    <p>Mandatory fields are marked *</p>
    <form name=”myForm” id="myForm" method =”post”>
      <table width="70%">
        <tr><td>Please enter your first name*</td><td>Please enter your last name*</td></tr>
        <tr><td><input id="fname" type="text" size="30"></td><td><input id="lname" type="text" size="30"></td></tr>
        <tr><td>Please enter your house number or name*</td><td> Please enter your postcode or zip code*</td></tr>
        <tr><td><input id="house" type="text"></td><td><input id="pcode" type="text"></td></tr>
        <tr><td>Please enter your telephone number*</td><td> Please enter a second contact number</td></tr>
        <tr><td><input id="telno" type="text"></td><td><input id="2nd_tel" type="text"></td></tr>
        <tr><td>Please enter your email address*</td><td> Please enter your email address again*<br><tr><td><input id="email" type="text"></td><td><input id="verifyEmail" type="text"></td></tr>
        <tr><td>Please enter your website URL*</td><td> Please enter your date of birth*</td></tr>|
        <tr><td><input id="url" type="text"></td><td><input id="dob" type="text"></td></tr>
        <tr><td>Click the button below to submit</td><td>Click the button below to clear</td></tr>
        <tr><td><input id="submit" type="button" value="submit" onclick="validate()"></td><td><input id="reset" type="reset" value="reset">
    </table>
    </form>
    </body>
    </html>

    You need to make sure your form has an id attribute and that all of your input fields do as well. It is good practice to include a reset button so that all fields can quickly be cleared. Nesting the elements of the form within a table ensures that the text and fields line up properly.  Using <noscript> tags to advise visitors to your site that JavaScript must be enabled in the event that it is switched off used to be common practice, but Service Pack 2 on Windows XP seems to interfere with the correct operation of these tags in MS Internet Explorer, so it may be just as well to include instructions for switching on JavaScript at the top of the form, along with any other instructions for use, rather than using these tags.

    More JavaScript Articles
    More By Dan Wellman


       · The email validation fails for long TLDs. Valid addresses such as me@mydomain.info...
       · Thanks for pointing those out, the validation routine passed all of the tets i threw...
       · var urlRegxp =...
       · The statement in your post looks fine, is there anything else on the page it could...
       · Thanks for reply. I found the problem and it might be of instest to future visitors...
       · Have you heard of any problems with the new info domain? Just wondering if the regex...
       · hii sir,this is kumar.till today i have lots of doubts on regular expression.but...
       · Thanks, glad you found the article interesting and useful :DDan Wellman
       · Hii Sir,THanks for those useful articles .My personal request is when ever you...
       · hi,your expression works goods and fine it helps me alot.thanks...
     

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