Home arrow JavaScript arrow Page 5 - 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) - 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.

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