13.1 What Is a Regular Expression?


A user is asked to fill out an HTML form and provide his name , address, and birth date. Before sending the form off to a server for further processing, a JavaScript program checks the form to make sure the user actually entered something, and that the information is in the requested format. We saw in the last chapter some basic ways that JavaScript can check form information, but now with the addition of regular expressions, form validation can be much more sophisticated and precise. Before getting into form validation, we will delve into regular expressions and how they work. If you are savvy with Perl regular expressions (or the UNIX utilities, grep, sed, and awk ), you may move rapidly through this section, since JavaScript regular expressions, for the most part, are identical to those found in Perl.

A regular expression is really just a sequence or pattern of characters that is matched against a string of text when performing searches and replacements . A simple regular expression consists of a character or set of characters that matches itself. The regular expression is normally delimited by forward slashes ; for example, /abc/ .

Like Perl, JavaScript [1] provides a large variety of regular expression metacharacters to control the way a pattern is found. The metacharacters are used to control the search pattern; you can look for strings containing only digits, only alphas, a digit at the beginning of the line followed by any number of alphas, a line ending with a digit, and so on. When searching for a pattern of characters, the possibilities of fine-tuning your search are endless.

[1] JavaScript 1.2, NES 3.0 JavaScript 1.3 added toSource() method. JavaScript 1.5, NES 6.0 added m flag, non-greedy modifier, non-capturing parentheses, lookahead assertions. ECMA 262, Edition 3.

Again, JavaScript regular expressions are used primarily to verify data input on the client side. When a user fills out a form and presses the submit button, the form is sent to a server, and then to a CGI script for further processing. Although forms can be validated by a CGI program, it is more efficient to take care of the validation before sending the script to the server. This is an important function of JavaScript. The user fills out the form and JavaScript checks to see if all the boxes have been filled out correctly, and if not, the user is told to re-enter the data before the form is submitted to the server. Checking the form on the client side allows for instant feedback, and less travelling back and forth between the browser and server. It may be that the CGI program does its own validation anyway, but if JavaScript has already done the job, it will still save time and inconvenience for the user. With the power provided by regular expressions, the ability to check for any type of input, such as e-mail addresses, passwords, social security numbers , and birthdates is greatly simplified.

The first part of this chapter delves into the construction of regular expressions and how to use regular expression metacharacters. The second part of the chapter applies this information to validating form input.



JavaScript by Example
JavaScript by Example (2nd Edition)
ISBN: 0137054890
EAN: 2147483647
Year: 2003
Pages: 150
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net