Form data validation, regular expressions

Strings matching patterns in parentheses are stored in an array


<!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/13/06 
--> 
<?php
$test = $_GET["input"];
?>
<html>
<head>
<title>
Form data processing	
</title>
</head>
<body>
<?php
// each expression in parentheses gets stored into a separate array 
// element
$pattern = "^([a-z0-9]+)@([a-z._]+)$";
if (!eregi($pattern, $test, $chunks)) {
   print "Invalid string<br/>\n";
}
else {
     foreach ($chunks as $chunk) {
	     print "$chunk<br/>\n";
     }
}
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/php_examples/regexp/ereg_parsing.php?input=elenam@morris.umn.edu (change the input in the link to test).

Using a function parse_url to parse a url


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- 
parse_url function in PHP
Author: Elena Machkasova elenam@morris.umn.edu 
Last modified: 4/13/06 
--> 
<?php
function display_url($url) {
	$chunks = parse_url($url); // chopping up the url
	print "<h3>Parsing url</h3>\n";
	print "<ul>";
	foreach ($chunks as $chunk_name => $chunk) {
		print "<li> $chunk_name is $chunk</li>";
	}
	print "</ul>\n";
}
?>
<html>
<head>
<title>
parse_url function in PHP
</title>
</head>
<body>
<?php
$url1 = "http://cda.mrs.umn.edu/~elenam/1101_spring06/examples/regexp.html";
$url2 = "http://rynite.morris.umn.edu/~elenam/php_examples/ifelse/days_switch.php?input=16";
$url3 = "http://cda.mrs.umn.edu/~elenam/1101_spring06/resources.html#examples";

// displaying all three urls
display_url($url1);
display_url($url2);
display_url($url3);
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/php_examples/regexp/parse_url.php

Validating a form data and displaying errors so that the user can resubmit

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Displaying error messages for forms
Author: Elena Machkasova
Last Modifed: 4/13/2006
-->
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$submit = $_POST["submit"]; // need this field so that we can check if the 
			    // user got to this page by submitting a form
?>
<html>
<head>
<title>
Displaying errors
</title>
</head>
<body>
<?php
$form_string="
	<form name=\"theform\" method=\"POST\" 
	action=\"http://rynite.morris.umn.edu/~elenam/php_examples/regexp/error_messages.php\">
	<table border=\"0\">
	<tr>
	<td>Enter your name:</td>
	<td>
	<input type = \"text\" name = \"name\">
	</td>
	</tr>
	<tr>
	<td>Enter your e-mail:</td>
	<td>
	<input type = \"text\" name = \"email\">
	</td>
	</tr>
	<tr>
	<td>
	<input type=\"submit\" name = \"submit\" value=\"submit\">
	</td>
	</tr>
	</table>
	</form><br/>
";

$style1 = "";
$style2 = "";
$error_style = "style = \"color: red\"";
$message = "";
$name_value = "";
$email_value = "";
$errors = false;

if (!isset($submit)) {
	print $form_string; // display the form
}
else {
	// start validation
	if (!eregi("^[a-z ]+$", $name)) {
		$style1 = $error_style;
		$message = $message."The name is empty or has illegal characters<br/>";
		$errors = true;
	} else {
		// we want to redisplay correct fields if the user needs
		// to resubmit
		$name_value = "value = \"$name\"";	
	}
	if (!eregi("^([a-z0-9]+)@([a-z._]+)$", $email)) {
		$style2 = $error_style;
		$message = $message."The e-mail is empty or has illegal characters<br/>";
		$errors = true;
	} else {
		// we want to redisplay correct fields if the user needs
		// to resubmit
		$email_value = "value = \"$email\"";	
	}
	if (!$errors) {
		print "Thank you, $name, for submitting your information!<br/>\n";
	} else {
		// priniting the form
		print "<form name=\"theform\" method=\"POST\" 
		action=\"http://rynite.morris.umn.edu/~elenam/php_examples/regexp/error_messages.php\">
		<table border=\"0\">
		<tr>
		<td $style1>Enter your name:</td>
		<td>
		<input type = \"text\" name = \"name\" $name_value>
		</td>
		</tr>
		<tr>
		<td $style2>Enter your e-mail:</td>
		<td>
		<input type = \"text\" name = \"email\" $email_value>
		</td>
		</tr>
		<tr>
		<td>
		<input type=\"submit\" name = \"submit\" value=\"submit\">
		</td>
		</tr>
		</table>
		</form><br/>";
		// printing the error message
		print "<p $error_style>$message You need to resubmit the form</p>";
	}
}
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/php_examples/regexp/error_messages.php
UMM CSci 1101