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.
Example:
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.
Example:
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.
Example:
<html> |
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
Example:
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.
Example:
var c = "my first program JavaScript"; var i = c.replace(/javascript/, "JavaScript"); |