JavaScript
  Home arrow JavaScript arrow Page 5 - 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) - More fun with escape codes


    (Page 5 of 5 )

     

    Clearly, you need some way to check for a string containing forward slashes when using literal regular expressions; because forward slashes form part of the regular expression syntax, you must again use escape codes. The same can be used to check for many things including full stops, brackets, braces, parentheses, pipes, asterisks and other things. To check for a valid URL, including the protocol, for example, you could use:

    var myRegxp = /http\/\/www\.mysite\.co\.uk/;

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

    which would write true. Obviously, this will only check that one site URL has been correctly entered; it will not check that any URL entered is in the correct format, but we will look at this also in part two.

    Another character that can be used in regular expressions is the pipe character | to say that either something OR something else should be matched. The check that a URL entered begins with http, https or www, you could use the following expression:

    var myRegxp = /^http|https|www/;

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

    The above example produces false as the URL does not begin with a required pattern, but add one of the protocols or www and it returns true.

    These are the basic building blocks of JavaScript regular expressions, and while they may not look like much, so much can be done with them. This will be covered in more detail in part two.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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