Elementary Form Validation : Form Validation « Form « PHP






Elementary Form Validation

 
<?php
     if(isset($_GET['submit'])) {
          if(preg_match("/^\(([2-9][0-9]{2})\)[2-9][0-9]{2}-[0-9]{4}$/i",
                                  $_GET['phone']) != 1) {
               echo "The phone field was invalid<BR>";
          }
     } else {
          /* Code to process form here */
     }
?>
<HTML>
<HEAD><TITLE>Elementary form validation</TITLE></HEAD>
<BODY>
<FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD=GET>
<INPUT TYPE="hidden" NAME="submit" VALUE="1">
Phone: <INPUT TYPE="text" NAME="phone" SIZE=13 MAXLENGTH=13>
(ex. (333)555-1212)<BR>
<INPUT TYPE="submit" VALUE="Send">
</FORM>
</HTML>
  
  








Related examples in the same category

1.Form value validation: not empty
2.Form Data Validation With Error Count
3.A Sample Form Element Validation Function
4.Displaying error messages with the form
5.Form Example for the Form Validator Script
6.Validating form data