Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb
"Accelerated DOM Scripting with Ajax, APIs, and Libraries"
Writing a regular expression to correctly trap all these cases would be impossible. There are also plenty of things that can??™t be checked with a regular expression at all: for example, freeform date fields, numbers with a range, or URLs (you can check that something looks like a URL, but not whether that URL actually works). For checking this sort of user entry, you need real code. Doing It on the Server It??™s pretty easy to put together a server-side form in which each field is tied to a particular function that validates what??™s entered into it. A little PHP example is shown in Listing 7-5. Listing 7-5. noajax-form.php demonstrates PHP server-side validation require_once "validation.php"; $ERRORS = Array(); if (isset($_GET["submit"])) { # form was submitted foreach ($_GET as $field => $data) { $check = validate($field, $data); if ($check != "") { $ERRORS[$field] = $check; } } if (count($ERRORS) == 0) echo "Data OK; now redirect!"; } ?> "http://www.w3.org/TR/html4/strict.dtd">
A simple PHP form using separate code for validation
A simple PHP form using separate code for validation
This code uses the $ERRORS approach and a form structure similar to the preceding regular expressions form, but it now calls a function validate() for each submitted form value.