Page 603 - Beginning PHP 5.3
P. 603
Chapter 18: String Matching with Regular Expressions
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<title>Validating Order Form Fields</title>
<link rel=”stylesheet” type=”text/css” href=”common.css” />
</head>
<body>
<h1>Validating Order Form Fields</h1>
<?php
if ( isset( $_POST[“submitted”] ) ) {
processForm();
} else {
displayForm();
}
function displayForm() {
?>
<h2>Please enter your order details below then click Send Order:</h2>
<form action=”” method=”post” style=”width: 30em;”>
<div>
<input type=”hidden” name=”submitted” value=”1” />
<label for=”emailAddress”>Your Email Address:</label>
<input type=”text” name=”emailAddress” id=”emailAddress” value=”” />
<label for=”phoneNumber”>Your Phone Number:</label>
<input type=”text” name=”phoneNumber” id=”phoneNumber” value=”” />
<label for=” productCodes”>Product Codes to Order:</label>
<input type=”text” name=”productCodes” id=”productCodes” value=”” />
<label> </label>
<input type=”submit” name=”submitButton” value=”Send Order” />
</div>
</form>
<div style=”clear: both;”> </div>
<p>(Separate product codes by commas. Codes are SW, MW, WW followed by 2
digits.)</p>
<?php
}
function processForm() {
$errorMessages = array();
$emailAddressPattern = “/
^ # Start of string
\w+((-|\.)\w+)* # Some word characters optionally separated by - or
# .
\@
[A-Za-z\d]+ # Domain name: some alphanumeric characters
565
9/21/09 6:18:02 PM
c18.indd 565
c18.indd 565 9/21/09 6:18:02 PM