JavaScript
  Home arrow JavaScript arrow Page 3 - 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 
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 / 97
    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) - Defining the regular expressions


    (Page 3 of 4 )

    Next, you need to define the regular expressions that the script will match against your visitors' input:

    var fnameRegxp = /^([a-zA-Z]+)$/;

    This statement checks that only upper or lowercase case letters, repeated one or more times, pass the validation test, which, unless you’re hoping to send your newsletter to C3PO, should be the case.  Remember when I mentioned that regular expressions can still return true if there are incorrect characters present, provided that the correct pattern of characters is somewhere within the string?  Putting the circumflex and dollar sign at the beginning and end of the regular expression ensures that this does not happen, and that the string is only valid if it contains just what you’re asking for.

    var lnameRegxp = /^([a-zA-Z]+)$/;

    var houseRegxp = /^([0-9A-Za-z]+)$/;

    These then check that the surname entered is also any upper or lowercase character repeated one or more times, and that the house name consists of just numbers and letters. You could have shortened this to /^([\w]+)$/ using the shorthand escape code for "any word character," but that would allow underscores to be used, which rarely feature in property names.  

    var pcodeRegxp = /^([A-Za-z]{1,2})([0-9]{2,3})([A-Za-z]
    {2})$/;

    var telnoRegxp = /^([0-9]{11})$/;

    I’ve used local examples for the post code (the UK version of a zip code) and telephone regular expressions. UK postcodes are in a format consisting of one or two letters, followed by two or three numbers (depending on the county), and followed again by two letters. It should be easy to see how you could change this to match your own local form of postal or zip code and telephone number formats. The phone number check simply ensures that the correct number of numbers is present. Following these comes the most complex of regular expressions -- those that check for valid email addresses and URLs:

    var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3})
    {1,2}$/;

    var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/
    \/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;

    Due to sub-domains, there may be any number of characters and dots preceding the @ sign. The first of these expressions says that any word character displayed one or more times can then be followed by a dot, then any number of word characters displayed zero or more times, followed by the @ symbol, followed by any word character displayed one or more times, followed by a dot and two or three word characters repeated at least once but no more than twice, so email addresses ending in .com or .co.uk will pass, whereas .co.uk.com would fail.  Similarly, the URL may begin with either http://www. or https://www. or ftp://www. or just www. once followed by any word character one or more times, followed by a dot and any number of word characters at least once but no more than twice. 

    Finally, the date of birth check allows dates in the format dd/mm/yyyy or dd-mm-yyyy, both formats being equally as popular:

    var dobRegxp = /^([0-9]){2}(\/|-){1}([0-9]){2}(\/|-)
    ([0-9]){4}$/;

    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...
       · 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...
       · An advantage not mentioned here of using client side validation is it makes forms...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







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