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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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