The form to test regular expressions


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- 
Examples of form handling for CSci 1101. 
Author: Elena Machkasova elenam@morris.umn.edu 
Last modified: 4/16/09
--> 
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>A form to test php regular expressions</title>
</head>
<body>

<form method="post" action="regex.php">

<p>Test field: <input type="text" name="test" value="" style="width: 100px" /></p>
<table style="border: none">
<tr>
<td>
Enter your name (Last, First)
</td>

<td>
<input type="text" name="name" value="" style="width: 100px" />
</td>

</tr>
<tr>
<td>
Enter your phone number in format (123) 456-7890
</td>
<td>

<input type="text" name="phone" value="" style="width: 100px" />
</td>
</tr>

<tr>
<td>
Enter your city, state
</td>
<td>
<input type="text" name="city" value="" style="width: 100px" />

</td>
</tr>
<tr>

<td>
Enter your zip code (5 digits)
</td>
<td>
<input type="text" name="zip" value="" style="width: 100px" />
</td>

</tr>
<tr>
<td>

<input type="submit" value="submit" />
</td>
</tr>
</table>
</form>

</body>
</html>

http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/regex/form.html

The file for processing regular expressions


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- 
Regular expressions in PHP
Author: Elena Machkasova elenam@morris.umn.edu 
Last modified: 4/17/06 
--> 
<?php
$test = $_POST["test"];
$name = $_POST["name"];
$phone = $_POST["phone"];
$city = $_POST["city"];
$zip = $_POST["zip"];
?>
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>

Form data processing	
</title>
</head>

<body>
<div>
<?php
print "$test <br/>\n";
print "$name $phone $city $zip<br/>\n";

print "<h3>Validating test field using different patterns</h3>\n";
// has a substring "red"
$pattern1 = "red";

// has a substring "red" or "blue"
$pattern2 = "red|blue";

// starts with "one" or "two"
$pattern3 = "^(one|two)";

// starts with "one", end with "two", can have anything in between
// . matches any character, * allows any number of them (or none)
$pattern4 = "^one.*two$";

// has a digit in it
$pattern5 = "[0-9]";

// has at least two digits in it
$pattern6 = "[0-9].*[0-9]";

// has one or more digits (+) in the beginning, followed by one or more letters
$pattern7 = "^[0-9]+[a-zA-Z]+$";

// has a three-symbol combination that starts with t, ends with p, and has 
// anything other than i in the middle.
$pattern8 = "t[^i]p";

// doesn't have digits (i.e. consists of non-digit symbols only)
// note that an empty string matches
$pattern9 = "^[^0-9]*$";

// See pp. 87-97 and pp. 288-304 for more

if (!ereg($pattern1, $test)) {
	print "Invalid string<br/>\n";
}
else {
	print "The string is valid<br/>\n";
}

print "<h3>Validating name</h3>\n";
// eregi is a case-insensitive version of ereg
if (!eregi("^[a-z]+,[ ]?[a-z]+$", $name)) {
	print "Invalid name<br/>\n";
}
else {
	print "The name is valid<br/>\n";
}

print "<h3>Validating phone number</h3>\n";
// parentheses are backslashed since they have a special meaning 
// in regular expression otherwise
if (!ereg("^\([0-9]{3}\)[ ]?[0-9]{3}-[0-9]{4}$", $phone)) {
	print "Invalid phone number<br/>\n";
}
else {
	print "The phone number is valid<br/>\n";
}

// write regular expression to validate city-state and zip code
?>

</div>
</body>
</html>

UMM CSci 1101