Home arrow JavaScript arrow Page 3 - Form Validation with JavaScript Regular Expressions (Part 1)

Form Validation with JavaScript Regular Expressions (Part 1)

Forms are one of the most useful ways to collect information online from customers and potential customers who visit your website. But that information is just a waste of electrons if it is not correct. How can you validate it? This article explains some of the building blocks you can use in JavaScript to at least ascertain that information entered into a form is in the expected format.

Author Info:
By: Dan Wellman
Rating: 4 stars4 stars4 stars4 stars4 stars / 130
May 10, 2005
  1. · Form Validation with JavaScript Regular Expressions (Part 1)
  2. · Regular expressions in JavaScript
  3. · Using RegExp
  4. · Other characters
  5. · More fun with escape codes

print this article

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*/;



var myRegxp = /w+/;


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}/;


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}/;


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.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials