Computer Science Canada validation |
Author: | alpesh [ Wed Aug 29, 2012 8:47 am ] |
Post subject: | validation |
Quote: <?php if (empty($_POST['name'])) // Determine whether a variable is empty { $errors[] = 'Please enter a name'; } if (empty($_POST['age'])) { $errors[] = 'Please enter a age'; } else if (!is_numeric($_POST['age'])) { $errors[] = 'Please enter a valid age with a numeric value'; } if (empty($_POST['email'])) { $errors[] = 'Please enter an e-mail'; } else if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) { $errors[] = 'Please enter a valid e-mail address'; // eregi ? Case insensitive regular expression match } if (empty($_POST['comments'])) { $errors[] = 'Please enter some comments'; } else if (strlen ($_POST['comments']) > 255) { $errors[] = 'Your comment is too long, please do not submit more then 255 characters'; } // count ? Count elements in an array, or properties in an object if (count($errors) == 0) { // Process form } else { echo $errors[0]; } ?> <form method="post"> <table> <tr> <td>Name:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Age:</td> <td><input type="text" name="age"></td> </tr> <tr> <td>E-mail:</td> <td><input type="text" name="email"></td> </tr> <tr> <td>Comments:</td> <td><input name="comments" ></td> </tr> <tr> <td colspan="2"><input type="submit" name="submit"></td> </tr> </table> </form> |