Learning PHP, MySQL & JavaScript by Robin Nixon

Learning PHP, MySQL & JavaScript by Robin Nixon

Author:Robin Nixon [Nixon, Robin]
Language: eng
Format: epub, mobi
Tags: www.it-ebooks.info
Published: 2015-01-14T00:23:21+00:00


If those two checks are satisfied, the test function is called to see whether any disal‐

lowed characters appear in the field. If any of these tests fail, an error message is

returned. The allowed characters in an email address are uppercase and lowercase let‐

ters, numbers, and the _, -, period, and @ characters, as detailed in the regular expres‐

sion passed to the test method. If no errors are found, the empty string is returned

to indicate successful validation. On the last line, the script and document are closed.

Figure 16-2 shows the result of the user clicking the Signup button without having

completed any fields.

Figure 16-2. JavaScript form validation in action

Using a separate JavaScript file

Of course, because they are generic in construction and could apply to many types of

validations you might require, these six functions make ideal candidates for moving

out into a separate JavaScript file. You could name the file something like vali‐

date_functions.js and include it right after the initial script section in Example 16-1,

using the following statement:

<script src="validate_functions.js"></script>

Regular Expressions

Let’s look a little more closely at the pattern matching we have been doing. We’ve

achieved it using regular expressions, which are supported by both JavaScript and

PHP. They make it possible to construct the most powerful of pattern-matching algo‐

rithms within a single expression.

Regular Expressions | 377

Matching Through Metacharacters

Every regular expression must be enclosed in slashes. Within these slashes, certain

characters have special meanings; they are called metacharacters. For instance, an

asterisk (*) has a meaning similar to what you have seen if you use a shell or Win‐

dows command prompt (but not quite the same). An asterisk means, “The text you’re

trying to match may have any number of the preceding characters—or none at all.”

For instance, let’s say you’re looking for the name Le Guin and know that someone

might spell it with or without a space. Because the text is laid out strangely (for

instance, someone may have inserted extra spaces to right-justify lines), you could

have to search for a line such as this:

The difficulty of classifying Le Guin's works

So you need to match LeGuin, as well as Le and Guin separated by any number of

spaces. The solution is to follow a space with an asterisk:

/Le *Guin/

There’s a lot more than the name Le Guin in the line, but that’s OK. As long as the

regular expression matches some part of the line, the test function returns a true

value. What if it’s important to make sure the line contains nothing but Le Guin? I’ll

show you how to ensure that later.

Suppose that you know there is always at least one space. In that case, you could use

the plus sign (+), because it requires at least one of the preceding characters to be

present:

/Le +Guin/

Fuzzy Character Matching

The dot (.) is particularly useful, because it can match anything except a newline.

Suppose that you are looking for HTML tags, which start with < and end with >. A

simple way to do so is shown here:

/<.*>/

The dot matches any character, and the * expands it to match zero or more charac‐

ters, so this is saying, “Match anything that lies between < and >, even if there’s noth‐

ing.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.