LERAN PHP COMPLETE

Learn one of the most powerful programming languages in the world and become a rockstar developer.

PHP Validation Example


Required field will check whether the field is filled or not in the proper way. Most of cases we will use the * symbol for required field.

What is Validation ?

Validation means check the input submitted by the user. There are two types of validation are available in PHP. They are as follows −

  • Client-Side Validation − Validation is performed on the client machine web browsers.

  • Server Side Validation − After submitted by data, The data has sent to a server and perform validation checks in server machine.

The validation rules for the form above are as follows:

Field Validation Rules
Name Required. + Must only contain letters and whitespace
E-mail Required. + Must contain a valid email address (with @ and .)
Website Optional. If present, it must contain a valid URL
Comment Optional. Multi-line input field (textarea)
Gender Required. Must select one

Example

<!-- Sample Source Code -->
<!DOCTYPE html>
<html>
  <html>
    <head>
      <title>PHP Information Registration
      </title>
    </head>
    <body>
      <?php
          $nameErr = $emailErr = $genderErr = $websiteErr = "";
          $name    = $email = $gender = $website = "";
          if ($_SERVER["REQUEST_METHOD"] == "POST") {
              if (empty($_POST["name"])) {
                  $nameErr = "Name is required";
              } else {
                  $name = test_input($_POST["name"]);
              }
              if (empty($_POST["email"])) {
                  $emailErr = "Email is required";
              } else {
                  $email = test_input($_POST["email"]);
                  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                      $emailErr = "Invalid email format";
                  }
              }
              if (empty($_POST["website"])) {
                  $website = "";
              } else {
                  $website = test_input($_POST["website"]);
              }
              if (empty($_POST["address"])) {
                  $address = "";
              } else {
                  $address = test_input($_POST["address"]);
              }
              if (empty($_POST["gender"])) {
                  $genderErr = "Gender is required";
              } else {
                  $gender = test_input($_POST["gender"]);
              }
          }
          function test_input($data)
          {
              $data = trim($data);
              $data = stripslashes($data);
              $data = htmlspecialchars($data);
              return $data;
          }
      ?>
      <h2>Adzetech Information registration
      </h2>
      <p>
        <span class = "error">* required field.
        </span>
      </p>
      <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <table>
          <tr>
            <td>Name:
            </td>
            <td>
              <input type = "text" name = "name">
            </td>
          </tr>
          <tr>
            <td>E-mail:
            </td>
            <td>
              <input type = "text" name = "email">
            </td>
          </tr>
          <tr>
            <td>Address:
            </td>
            <td>
              <input type = "text" name = "address">
            </td>
          </tr>
          <tr>
            <td>Website:
            </td>
            <td>
              <input type="url" name = "website" >
            </td>
          </tr>
          <tr>
            <td>Gender:
            </td>
            <td>
              <input type = "radio" name = "gender" value = "female">Female
              <input type = "radio" name = "gender" value = "male">Male
            </td>
          </tr>
          <tr>
            <td>
              <input type = "submit" name = "submit" value = "Submit">
            </td>
          </tr>
        </table>
      </form>
      <?php
          echo "<h2>Your Given details are as :</h2>";
          echo $name;
          echo "<br>";
          echo $email;
          echo "<br>";
          echo $website;
          echo "<br>";
          echo $address;
          echo "<br>";
          echo $gender;
       ?>
    </body>
  </html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>ctrlq
    </title>
  </head>
  <body>
    <h1>Hello World
    </h1>
  </body>
</html> 

Output

form validation php example

x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine