JavaScript
  Home arrow JavaScript arrow Page 4 - 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 / 90
    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) - Testing the values submitted


    (Page 4 of 4 )

    We then need to actually test that each of the values submitted are in the correct format. I accomplished this by using a series of nested if statements and alerts. This method is useful for demonstration and testing purposes, although in reality, a for…next loop would probably be more efficient, and some kind of color highlighting scheme that would flag the erroneous input values a different color, say red, on the form itself would be more professional. The example if statements and alerts however, can be constructed as follows:

    if (fnameRegxp.test(fname) != true)

      alert("First name appears to be incorrect");

        if (lnameRegxp.test(lname) != true)

          alert("Last name appears to be incorrect");

            if (houseRegxp.test(house) != true)

              alert("Address 1 appears to be incorrect");

                if (pcodeRegxp.test(pcode) != true)

                  alert("Address 2 appears to be incorrect");

                    if (telnoRegxp.test(telno) != true)

                      alert("Telephone number appears to be
    incorrect");

                        if (emailRegxp.test(email) != true)

                          alert("Email address appears to be
    incorrect");

                            if (email != verEmail)

                              alert("Email appears to be
    incorrect");

                                if (urlRegxp.test(url) != true)

                                  alert("URL appears to be
    incorrect");

                                    if (dobRegxp.test(dob) !=
    true)

                                      alert("Date of Birth
    appears to be incorrect");

    Notice that instead of using a regular expression to check that the second email address entered (that should be the same as the first for verification purposes) is correct, we simply check that its value is exactly the same as the first email address entered. 

    Finally, if all of the data is in the correct format, we need this program to output a "Data Correct" alert and change the value of the action property of the form to the name of the cgi function that will process the data. Once again, against a professional backdrop, the true alert would probably be removed in favor of a fresh page thanking the visitor for their time, but this is useful for demonstration and testing purposes:

    else {

      alert("Data Correct");

      document.myForm.action.value = "process.cgi";

      }

    }

    </script>

    Another benefit to using regular expressions to validate user input is that any fields that are checked against a regular expression fail if the field is left blank, so you don’t have to write any separate omission checking functions.

    Explaining regular expressions is almost as difficult as coding them. It will be far easier for you to see what I mean by writing and playing around with them yourself. Unfortunately, it is not possible to be 100 percent certain that all information entered is correct, but with regular expressions you can at least be sure that 99 percent of the data is correct. 

    Although this may seem like a cumbersome amount of code, without regular expressions, it would be ten times as long.  Using client-side validation is not the securest way to validate your forms, but it may save processing time on your server by ensuring that only correct data is passed to it in the first place. Other than that, JavaScript is both quick and easy to implement and the regular expressions subset of the language is simpler than that of some of the more powerful Web programming languages, so for sites that don’t need maximum security, it is certainly worth considering. 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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...
       · Dan, your email regex : var emailRegxp =...
       · Hi, thanks for pointing this out and useful to know that escaping the period could...
       · What I mean't was it should have caught abcd@abcd as an invalid email format but did...
       · oh, ok, excellent, thanks for providing the fix in that case :DDan Wellman
       · i got a project of web designing and it was the phase of form validation i found ur...
     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - 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...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





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