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 1)
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 100
    2005-05-10

    Table of Contents:
  • Form Validation with JavaScript Regular Expressions (Part 1)
  • Regular expressions in JavaScript
  • Using RegExp
  • Other characters
  • More fun with escape codes

  • 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 1) - Using RegExp


    (Page 3 of 5 )

    When making use of the constructor RegExp object, the extremely basic example above can be expressed using the following syntax:

    var myRegxp = new RegExp("www");

    In a very similar manner, you just use the new keyword in conjunction with the RegExp object and substitute the forward slashes for parentheses and double quotes. To use one of the flags, just add the flag inside another set of double quotes and separate with a comma:

    var myRegxp = new RegExp("www", "i");

    It is also possible to denote repetition of specified characters too; you can use the asterisk symbol after a particular character to specify zero or more times, or the plus symbol to specify one or more:

    var myRegxp = /w*/;

    document.write(myRegxp.test("test"))

    or

    var myRegxp = /w+/;

    document.write(myRegxp.test("test"))

    The first of these examples will produce true, whereas the second will produce false.

    You can also use the question mark to state that something should appear zero or one time, and you can use braces to state exactly how many times something should occur:

    var myRegxp = /w{3}/;

    document.write(myRegxp.test("www"))

    which, of course, outputs true. You can also use a comma to separate values to indicate a minimum and maximum range of repetition. Note however that the following code still outputs true:

    var myRegxp = /w{3}/;

    document.write(myRegxp.test("wwww"))

    because there is a string of three w's contained within the string of four w's. This will be examined and overcome in part two.

    More JavaScript Articles
    More By Dan Wellman


       · > and even whitespace (using \w) or non-whitespace (using \W)\s and \S are used...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - 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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek