02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

320<br />

CHAPTER 9 ■ PERFORMING FORM VALIDATION WITH REGULAR EXPRESSIONS<br />

Figure 9-4. A more complex replacement<br />

■ Note The remaining examples in this section will use only regexes.<br />

Matching Character Classes<br />

In some cases, it’s desirable to match more than just a word. For instance, sometimes you want to verify<br />

that only a certain range of characters was used (i.e., to make sure only numbers were supplied for a<br />

phone number or that no special characters were used in a username field).<br />

Regexes allow you to specify a character class, which is a set of characters enclosed in square<br />

brackets. For instance, to match any character between the letter a <strong>and</strong> the letter c, you would use [a-c]<br />

in your pattern.<br />

You can modify regex.php to highlight any character from A-C. Additionally, you can move the<br />

pattern into a variable <strong>and</strong> output it at the bottom of the sample data; this helps you see what pattern is<br />

being used when the script is loaded. Add the code shown in bold to accomplish this:<br />

<br />

<br />

<br />

<br />

Regular Expression Demo

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!