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  
Moblin 
JMSL Numerical Library 
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 1)
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 87
    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

    - 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
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






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