Share on Google+Share on Google+

Form Validation using Regular Expressions is JavaScript

In this article you learn the basics of JavaScript and create your first JavaScript program.

Form Validation using Regular Expressions is JavaScript


In this article you learn the basics of JavaScript and create your first JavaScript program.

What is JavaScript validation using Regular Expressions?
The JavaScript Validating user input is the bane of every software developer?s existence. The developing cross-browser web applications this task becomes even less enjoyable due to the lack of useful intrinsic validation functions in JavaScript. JavaScript 1.2 has incorporated regular expressions. This article I will present a brief tutorial on the basics of regular expressions and then give some examples of how they can be used to simplify data validation. A demonstration page and code library of common validation functions has been included to supplement the examples in the article. 

Regular Expression:
JavaScript regular Expression is the very powerful tool and performing for the patterns  matches. the PERL programmers and UNIX shell programmers have enjoyed the benefits of regular expressions for years. Once you  are master the pattern language, most validation tasks become trivial. if you perform complex tasks that once required lengthy procedures with just a few lines of code using regular expressions. 
The regular Expression two intrinsic objects associated with program. 
The RegExp object and 
The Regular Expression object.
The RegExp object is the parent in regular expression object. RegExp has a constructor function that is instantiates of the Regular Expression object much like the Date object instantiates an new date.


     Var RegularExpression = new RegExp("pattern", ["switch"])

The JavaScript has been creating alternet  syntax for Regular Expression objects. There are implicitly calls the RegExp constructor function. The syntax for the follows: 

  var RegularExpression =  /pattern/[switch]

Using JavaScript Validation
The JavaScript validation  offered the way if the field contained certain characters using the index Of() method. The character was found, the position of the character was returned as a number.


  var x = "my program's contents"; 
  var y = x.indexOf("my")

The JavaScript validation using indexOf(), you would be required to write several lines of code, each using the indexOf() to look for all the characters you didn't want to find. If an illegal character is found, an alert box could be flashed asking the user to re-enter their information. The  functions use JavaScript 1.0 functionality to examine the text field containing regular text or a text field containing an email address. By passing the contents of the form to the isReady() function using the on Submit event handler, the information is validated before being sent to the server. validation function is the returns true, the ACTION attribute of the form is run. 


<script language="JavaScript">
 function isEmail(string
 if (!string) return false; var iChars = "*|,\":<>[]{}`\';()&$#%";
 for (var i = 0; i < string.length; i++) 
 if (iChars.indexOf(string.charAt(i)) != -1) return false; 

return true;

Using Javascript Regular Expression:
The JavaScript 1.2 the way through the power of regular expressions. These expressions, which are offer the same functionality as regular expressions taken from Perl is a very popular scripting language, add the ability to parse form field input in ways that were simply not possible before. The Netscape Navigator 4.0x and Internet Explorer 4, illuminate the power associated with the new additions. It is JavaScript contains a number of new constructors and methods the allow a programmer to string of text using regular expressions. The first thing must be before you can begin parsing a string is to determine exactly regular expression will be. There are two way follows:-
The first is to specify it by hand using normal syntax, and 
The second is to use the new RegExp() constructor


    pattern = /:+/; 
    pattern = new RegExp(":+"); 

The JavaScript  replace() method allows a programmer to replace a found match with another string. that takes two arguments, one being the regular expression if you want to searched for, and the other being the replacement text you want substituted.


    var c = "my first program JavaScript"; 
  var i = c.replace(/javascript/, "JavaScript"); 



Posted on: December 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Form Validation using Regular Expressions is JavaScript  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 23, 2011

i want to validation following field name by text box email by text box country by select option, if select any country, state and city select option will display, if wise it ill be in default mode. and display the result in the same page
October 5, 2011

December 13, 2011

March 11, 2013

Samrat Adhikari
April 28, 2012

September 4, 2012

javascript program to accept name and age of a person using form and validate both of them using regular expression