SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 217 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

addEvent(window, "load", validator.init);



A simple PHP form using regular expressions for validation



function field($name, $text) {
global $ERRORS;
echo "

\n";
echo "\n";
if (array_key_exists($name,$ERRORS)) {
$err = $ERRORS[$name];
echo "$err";
} else {
echo "";
}
echo "

\n";
}
CHAPTER 7 n FORM VALIDATION AND JAVASCRIPT 155
field("firstname", "First name");
field("lastname", "Last name");
field("heads", "Number of heads");
field("dob", "Date of birth (DD/MM/YYYY)");
field("email", "Email address of someone you don't like for spamming purposes");
?>





And there you have it: form validation with regular expressions, progressively enhanced
to use DOM scripting.
Now that there??™s an infrastructure in place to do the validation of this form, there are
numerous ways to add extra enhancements. Here I??™ll discuss two: having the page add the
error span itself (instead of requiring it to already be present in the HTML) and preventing
the form being submitted if there are errors.
Adding the Error Span from JavaScript
In Listing 7-2, the PHP server code writes out a for each field,
even when there aren??™t any errors, as a placeholder for the error message.


Pages:
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
druga wojna światowa Free English grammar and study guid hotel jelenia góra Russian bride counter strike 1.6