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  
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) - Other characters


    (Page 4 of 5 )

     

    In addition to the flags and repetition quantifiers, there are many other characters that can be used to denote specific things. Positional indicators for example, can be used to state that a specific pattern should appear at the beginning or end of a string. A circumflex checks that the pattern is at the beginning of the string and a dollar sign is used to match the pattern at the end of a string:

    var myRegxp = /^www/;

    document.write(myRegxp.test("www.mysite.co.uk"));

    would output true whereas

    var myRegxp = /www$/;

    document.write(myRegxp.test("www.mysite.co.uk"));

    outputs false. As well as using the circumflex and dollar sign, escape codes can be used to check for other particular characters within strings.

    Both parentheses and square brackets can be used to signify different things. First, standard brackets represent grouped characters and square brackets indicate a particular class of characters such as digits or letters. For example, if you wanted to match a pattern consisting of alphanumeric characters, you could use:

    var myRegxp = /[0-9a-zA-Z]/

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

    As the @ character is not an alphanumeric character, it would output false. Negative classes can also be used by making use again of the circumflex character within the square brackets. Negative classes specify what cannot be included, so by adding the circumflex inside the first square bracket, we can say that any character can be included except for alphanumeric characters:

    var myRegxp = /[^0-9a-zA-Z]/

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

     

    which now displays true. Using character classes can quickly become cumbersome and increase the size of your script code. To counteract this, JavaScript provides shorthand escape codes that can be used to check for any digits or non-digits, and word or non-word characters, and so forth. The above expression could easily be rewritten as

    var myRegxp = /[\D\W]/;

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

    which checks for any non-digit or any non-word characters. True is still the result. When using escaped character classes, a lowercase letter signifies that you're checking that the character does appear, and an upper case letter checks that it does not appear, so to check for numbers, use \d and to check for non-numbers, use \D. These shorthand escapes can be used to match numbers (or non-numbers), words (or non-words), and even whitespace (using \w) or non-whitespace (using \W) so you can see that most strokes of the keyboard can be validated in some way or another.

    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 4 hosted by Hostway
    Stay green...Green IT