PHP/Form/Form Validation

Материал из Web эксперт
Перейти к: навигация, поиск

A Sample Form Element Validation Function

 
<?php
    function phone_validate($data, $desc) {
        $regex = "/^\([2-9][0-9]{2}\)[2-9][0-9]{2}-[0-9]{4}/i";
        if(preg_match($regex, $data) != 1) {
            return "The "$desc" field isn"t valid!";
        }
        return true;
    }
?>



Displaying error messages with the form

 
<?
if ($_POST["_submit_check"]) {
    if ($form_errors = validate_form()) {
        show_form($form_errors);
    } else {
        process_form();
    }
} else {
    show_form();
}
function process_form() {
    print "Hello, ". $_POST["my_name"];
}
function show_form($errors = "") {
    if ($errors) {
        print "Please correct these errors: <ul><li>";
        print implode("</li><li>", $errors);
        print "</li></ul>";
    }
    print<<<_HTML_
<form method="POST" action="$_SERVER[PHP_SELF]">
Your name: <input type="text" name="my_name">
<br/>
<input type="submit" value="Say Hello">
<input type="hidden" name="_submit_check" value="1">
</form>
_HTML_;
}
function validate_form() {
    $errors = array();
    if (strlen($_POST["my_name"]) < 3) {
        $errors[] = "Your name must be at least 3 letters long.";
    }
    return $errors;
}
?>



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>



Form Data Validation With Error Count

<HTML>
<BODY>
<FORM METHOD="POST" ACTION="FormDataValidationWithErrorCount.php">
<H1>Contact Information</H1>
<TABLE>
<TR>
  <TD><B>Nickname:</B></TD>
  <TD><INPUT TYPE="TEXT" NAME="nickname"></TD>
</TR>
<TR>
  <TD><B>First Name:</B></TD>
  <TD><INPUT TYPE="TEXT" NAME="firstname"></TD>
</TR>
<TR>
  <TD>Middle Name:</TD>
  <TD><INPUT TYPE="TEXT" NAME="middlename"></TD>
</TR>
<TR>
  <TD><B>Last Name:</B></TD>
  <TD><INPUT TYPE="TEXT" NAME="lastname"></TD>
</TR>
<TR>
  <TD><B>Primary Email:</B></TD>
  <TD><INPUT TYPE="TEXT" NAME="email"></TD>
  <TD WIDTH="20">&nbsp;</TD>
  <TD>Secondary Email:</TD>
  <TD><INPUT TYPE="TEXT" NAME="secondaryemail"></TD>
</TR>
<TR>
  <TD></TD>
  <TD><INPUT TYPE="TEXT" NAME="officeaddress2"></TD>
</TR>
<BR>
<BR>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<BR>
<BR>
<INPUT TYPE="RESET"  VALUE="Clear the Form">
</FORM>
</BODY>
</HTML>

<!-- FormDataValidationWithErrorCount.php
<?php
  function validate_form(){
      global $nickname, $firstname, $lastname, $email;
      $errors=0;
      if (!trim($nickname)) 
      {
          echo "<BR><B>Nickname</B> is required.";
          $errors++;
      }
     
      if (!trim($firstname))
      {
          echo "<BR><B>First name</B> is required.";
          $errors++;
      }
     
      if (!trim($lastname))
      {
          echo "<BR><B>Last name</B> is required.";
          $errors++;
      }
     
      if (!trim($email))
      {
          echo "<BR><B>Primary email address</B> is required.";
          $errors++;
      }
    
      switch ($errors){
          case 0:
                  return TRUE;
          case 1:
                  echo "<BR><BR><BR>Please use your ";
                  echo "browser"s back button to return to ";
                          echo "the form, correct the error, and ";
                          echo "re-submit the form.";
                  return FALSE;
          default:
                  echo "<BR><BR><BR>Please use your ";
                  echo "browser"s back button to return to ";
                          echo "the form, correct the errors, and ";
                          echo "re-submit the form.";
                  return FALSE;
      }
}
function update_database(){
   echo "<BR>Updating database....";
}
$ok = validate_form();
if ($ok)
    update_database();
?>
</BODY>
</HTML>

-->



Form Example for the Form Validator Script

 
<?php if($form_errors): ?>
<UL>
<?php foreach($form_errorlist as $val): ?>
<LI><?php echo $val;?>
<?php endforeach; ?>
</UL>
<?php endif; ?>
Please fill out the following form (* = Required)<BR>
<FORM ACTION="<?php echo $_SERVER["PHP_SELF"]; ?>" METHOD=GET>
<INPUT TYPE="hidden" NAME="submit" VALUE="1">
<INPUT TYPE="hidden" NAME="required" VALUE="phone,email,fax">
<INPUT TYPE="hidden" NAME="phone_desc" VALUE="Phone Number">
<INPUT TYPE="hidden" NAME="email_desc" VALUE="Email Address">
<INPUT TYPE="hidden" NAME="fax_desc" VALUE="Fax Number">
Your Name: <INPUT TYPE="text" NAME="name"><BR>
* Your Phone Number:
<INPUT TYPE="text" NAME="phone" VALUE="<?php echo $_GET["phone"]; ?>"><BR>
* Your Email Address:
<INPUT TYPE="text" NAME="email" VALUE="<?php echo $_GET["email"]; ?>"><BR>
* Your Fax Number:
<INPUT TYPE="text" NAME="fax" VALUE="<?php echo $_GET["fax"]; ?>"><BR>
<INPUT TYPE="submit" VALUE="Send">
</FORM>



Form value validation: not empty

<HTML>
<BODY>
<FORM METHOD="POST" ACTION="FormErrorCheck.php">
<H1>Contact Information</H1>
<TABLE>
<TR>
  <TD><B>Nickname:</B></TD>
  <TD><INPUT TYPE="TEXT" NAME="nickname"></TD>
</TR>
<TR>
  <TD>Title:</TD>
  <TD><INPUT TYPE="TEXT" NAME="title"></TD>
</TR>
<TR>
  <TD><B>First Name:</B></TD>
  <TD><INPUT TYPE="TEXT" NAME="firstname"></TD>
</TR>
<TR>
  <TD>Middle Name:</TD>
  <TD><INPUT TYPE="TEXT" NAME="middlename"></TD>
</TR>
<TR>
  <TD><B>Last Name:</B></TD>
  <TD><INPUT TYPE="TEXT" NAME="lastname"></TD>
</TR>
<TR>
  <TD><B>Primary Email:</B></TD>
  <TD><INPUT TYPE="TEXT" NAME="email"></TD>
  <TD WIDTH="20">&nbsp;</TD>
  <TD>Secondary Email:</TD>
  <TD><INPUT TYPE="TEXT" NAME="secondaryemail"></TD>
</TR>
<TR>
  <TD>Company Name:</TD>
  <TD><INPUT TYPE="TEXT" NAME="companyname"></TD>
</TR>
<TR>
  <TD>Office Address:</TD>
  <TD><INPUT TYPE="TEXT" NAME="officeaddres1"></TD>
  <TD WIDTH="20">&nbsp;</TD>
  <TD>Home Address:</TD>
  <TD><INPUT TYPE="TEXT" NAME="homeaddress"></TD>
</TR>
<TR>
  <TD></TD>
  <TD><INPUT TYPE="TEXT" NAME="officeaddress2"></TD>
</TR>
<TR>
  <TD>City:</TD>
  <TD><INPUT TYPE="TEXT" NAME="officecity"></TD>
  <TD WIDTH="20">&nbsp;</TD>
  <TD>&nbsp;</TD>
  <TD><INPUT TYPE="TEXT" NAME="homecity"></TD>
</TR>
<TR>
  <TD>State:</TD>
  <TD><INPUT TYPE="TEXT" NAME="officestate"></TD>
  <TD WIDTH="20">&nbsp;</TD>
  <TD>&nbsp;</TD>
  <TD><INPUT TYPE="TEXT" NAME="homestate"></TD>
</TR>
<TR>
  <TD>Zip:</TD>
  <TD><INPUT TYPE="TEXT" NAME="officezip"></TD>
  <TD WIDTH="20">&nbsp;</TD>
  <TD>&nbsp;</TD>
  <TD><INPUT TYPE="TEXT" NAME="homezip"></TD>
</TR>
<TR>
  <TD>Phone:</TD>
  <TD><INPUT TYPE="TEXT" NAME="officephone"></TD>
  <TD WIDTH="20">&nbsp;</TD>
  <TD>&nbsp;</TD>
  <TD><INPUT TYPE="TEXT" NAME="homephone"></TD>
</TR>
<TR>
  <TD>Birthday:</TD>
  <TD><INPUT TYPE="TEXT" NAME="birthday"></TD>
</TR>
<TR>
  <TD>Spouse Name:</TD>
  <TD><INPUT TYPE="TEXT" NAME="spousename"></TD>
  <TD WIDTH="20">&nbsp;</TD>
  <TD>Childrens" Names:</TD>
  <TD><INPUT TYPE="TEXT" NAME="children"></TD>
</TR>
<TR>
  <TD>Anniversary:</TD>
  <TD><INPUT TYPE="TEXT" NAME="anniversary"></TD>
</TR>
</TABLE>
<BR>
<BR>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<BR>
<BR>
<INPUT TYPE="RESET"  VALUE="Clear the Form">
</FORM>
</BODY>
</HTML>
<!-- FormErrorCheck.php
<HTML>
<BODY>
<?php
  $errors=0;
  if (!trim($nickname)) {
      echo "<BR><B>Nickname</B> is required.";
     $errors++;
  }
 
  if (!trim($firstname)) {
      echo "<BR><B>First name</B> is required.";
     $errors++;
  }
 
  if (!trim($lastname)) {
      echo "<BR><B>Last name</B> is required.";
      $errors++;
  }
 
  if (!trim($email)) {
      echo "<BR><B>Primary email address</B> is required.";
      $errors++;
  }
  if ($errors > 0)
      echo "<BR><BR><BR>Please use your browser"s back button " .
        "to return to the form, and correct error(s)";
 
?>
</BODY>
</HTML>
-->



Validating form data

 
<?
if ($_POST["_submit_check"]) {
    if (validate_form()) {
        process_form();
    } else {
        show_form();
    }
} else {
    show_form();
}
function process_form() {
    print "Hello, ". $_POST["my_name"];
}
function show_form() {
    print<<<_HTML_
<form method="POST" action="$_SERVER[PHP_SELF]">
Your name: <input type="text" name="my_name">
<br/>
<input type="submit" value="Say Hello">
<input type="hidden" name="_submit_check" value="1">
</form>
_HTML_;
}
function validate_form() {
    if (strlen($_POST["my_name"]) < 3) {
        return false;
    } else {
        return true;
    }
}
?>