Form data validation, regular expressions


<html>
<body>

<form name="theform" method="POST" 
action="http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/regexp/regexp.php">

Test field: <input type="text" name="test" value="" width="30">
<table border="0">
<tr>
<td>
Enter your name (Last, First)
</td>
<td>
<input type="text" name="name" value="" width="30">
</td>

</tr>
<tr>
<td>
Enter your phone number in format (123) 456-7890
</td>
<td>
<input type="text" name="phone" value="" width="12">
</td>
</tr>

<tr>
<td>
Enter your city, state
</td>
<td>
<input type="text" name="city" value="" width="30">
</td>
</tr>
<tr>

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

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

</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/regexp/form.html

The php file that validates the form data


<!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/11/06 
--> 
<?php
$test = $_POST["test"];
$name = $_POST["name"];
$phone = $_POST["phone"];
$city = $_POST["city"];
$zip = $_POST["zip"];
?>
<html>
<head>
<title>

Form data processing	
</title>
</head>
<body>
<?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).*(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($pattern9, $test)) {
	print "Invalid string<br/>\n";
}
else {
	print "The string is valid<br/>\n";
}

print "<h3>Validating name</h3>\n";
// eregi is a case-insesitive 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
?>

</body>
</html>

UMM CSci 1101