JavaScript
  Home arrow JavaScript arrow Page 2 - 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) - Regular expressions in JavaScript


    (Page 2 of 5 )

    Regular expressions are not just a feature of JavaScript; although I will be using JavaScript as an example, you will come across regular expressions in almost any programming language, including PHP, VB Script, ASP.NET, Java and PERL. Regular expressions are a powerful and versatile approach to form validation, so the knowledge gained here can be applied by any Web developer in almost any popular programming language.

    The RegExp object is one of JavaScript's built in objects, and has methods and properties like the other built in objects: string, array and the like. You can create a new RegExp object in either a literal form or by using the RegExp() constructor.

    The code for a literal regular expression is pretty basic; just define a variable whose value is encapsulated within single forward slashes:

    var myRegexp = /www/;

    This is a regular expression in its most basic form, and to go with it, we can make use of its most basic method; the .test method:

    var myRegxp = /www/;

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

    This would simply write 'true' to the page. There are a few other, very basic things we could do with this using flags. The regular expression in the above example is case sensitive, so changing the www in the first line to WWW would output 'false' instead. To make the regular expression case insensitive, you just add i directly after the initial declaration:

    var myRegxp = /WWW/i;

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

    which again, writes 'true'. Other flags that may be used are the g or global flag, and the m or multi-line flag.

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek