Simple Registration Form Validation in PHP

Language

This project source code will teach you how to validate forms. Each field in the registration form has its own validation which is the First Name and Last Name has a null or empty validation. These validations will check if the fields are not empty or null. For the Email, upon submitting the registration form the system will check if the value entered in the Email field is a valid email. Then for the Password, the system will check if the entered characters length in this field is at least 8 characters. Lastly, the Confirm Password will check if the entered value in Password Field is identical equals to the value of this field. Using the Confirm Password validation will help the user to ensure that the password he/she entered is correct since the characters in the Password Field is not readable.

Demo

Example Scripts

Registration Form

  1. <form method="post" action="index.php">
  2. <tr>
  3. <td colspan="2">
  4. <!-- This PHP script is will display the success message of the registration -->
  5. <?php if(isset($successful)){ echo $successful; } ?>
  6. </td>
  7. </tr>
  8. <tr>
  9. <td><input type="text" name="fname" id="fname" placeholder="First Name" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];} ?>"></td>
  10. <td><input type="text" name="lname" id="lname" placeholder="Last Name" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];} ?>"></td>
  11. </tr>
  12. <tr>
  13. <td>
  14. <!-- This PHP script will display when the form is submitted in First Name field is empty -->
  15. <?php if(isset($errors['fname'])){echo "<h2>" .$errors['fname']. "</h2>"; } ?>
  16. </td>
  17. <td>
  18. <!-- This PHP script will display when the form is submitted in Last Name field is empty -->
  19. <?php if(isset($errors['lname'])){echo "<h2>" .$errors['lname']. "</h2>"; } ?>
  20. </td>
  21. </tr>
  22. <tr>
  23. <!-- The PHP Script in value attribute of the input below is use for value retrieval when registration fails due to validations -->
  24. <td colspan="2"><input type="text" name="email" id="email" placeholder="E-mail Address" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>"></td>
  25. </tr>
  26. <tr>
  27. <td colspan="2"><?php if(isset($errors['email'])){echo "<h2>" .$errors['email']. "</h2>"; } ?></td>
  28. </tr>
  29. <tr>
  30. <td colspan="2"><input type="password" name="password" id="pw" placeholder="Password" value="<?php if(isset($_POST['password'])){echo $_POST['password'];} ?>"></td>
  31. </tr>
  32. <tr>
  33. <td colspan="2"><?php if(isset($errors['password'])){echo "<h2>" .$errors['password']. "</h2>"; } ?></td>
  34. </tr>
  35. <tr>
  36. <td colspan="2"><input type="password" name="confirm_password" id="cpw" placeholder="Confirm Password" value="<?php if(isset($_POST['confirm_password'])){echo $_POST['confirm_password'];} ?>">
  37. </tr>
  38. <tr>
  39. <td colspan="2"><?php if(isset($errors['confirm_password'])){echo "<h2>" .$errors['confirm_password']. "</h2>"; } ?></td>
  40. </tr>
  41. <tr>
  42. <td><input type="submit" name="submit" id="submit" value="Sign Up"></td>
  43. </tr>
  44. </table>
  45. </form>

Registration Validation and data inserting to database

  1. <?php
  2. $con = mysqli_connect("localhost","root","","form_validation");
  3.  
  4. $errors = array();
  5.  
  6. if($_SERVER['REQUEST_METHOD'] == 'POST'){
  7.  
  8. if(preg_match("/\S+/", $_POST['fname']) === 0){
  9. $errors['fname'] = "* First Name is required.";
  10. }
  11. if(preg_match("/\S+/", $_POST['lname']) === 0){
  12. $errors['lname'] = "* Last Name is required.";
  13. }
  14. if(preg_match("/.+@.+\..+/", $_POST['email']) === 0){
  15. $errors['email'] = "* Not a valid e-mail address.";
  16. }
  17. if(preg_match("/.{8,}/", $_POST['password']) === 0){
  18. $errors['password'] = "* Password Must Contain at least 8 Chanacters.";
  19. }
  20. if(strcmp($_POST['password'], $_POST['confirm_password'])){
  21. $errors['confirm_password'] = "* Password do not much.";
  22. }
  23.  
  24. if(count($errors) === 0){
  25. $fname = mysqli_real_escape_string($con, $_POST['fname']);
  26. $lname = mysqli_real_escape_string($con, $_POST['lname']);
  27. $email = mysqli_real_escape_string($con, $_POST['email']);
  28.  
  29. $password = hash('sha256', $_POST['password']);
  30. function createSalt(){
  31. $string = md5(uniqid(rand(), true));
  32. return substr($string, 0, 3);
  33. }
  34. $salt = createSalt();
  35. $password = hash('sha256', $salt . $password);
  36.  
  37. $search_query = mysqli_query($con, "SELECT * FROM members WHERE email = '$email'");
  38. $num_row = mysqli_num_rows($search_query);
  39. if($num_row >= 1){
  40. $errors['email'] = "Email address is unavailable.";
  41. }else{
  42. $sql = "INSERT INTO members(`fname`, `lname`, `email`, `salt`, `password`) VALUES ('$fname', '$lname', '$email', '$salt', '$password')";
  43. $query = mysqli_query($con, $sql);
  44. $_POST['fname'] = '';
  45. $_POST['lname'] = '';
  46. $_POST['email'] = '';
  47.  
  48. $successful = "<h3> You are successfully registered.</h3>";
  49. }
  50. }
  51. }
  52. ?>

Download the source code by clicking the "Download" button below this article for the complete source code of this simple Registration Form Validation.

How to run the source Code

  1. Download the source code and extract the zip file.
  2. Download or set up any local web server that runs PHP script.
  3. Open the web-server database and create a new database name it "form_validation".
  4. Import the SQL file located in the "db" folder of the source code.
  5. Copy and paste the source code to the location where your local web server accessing your local projects. Example for XAMPP('C:\xampp\htdocs')
  6. Open a web browser and browse the project. E.g [http://localhost/form_validation]

I hope this will help you. Enjoy Coding :)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted byunic (not verified)on Sat, 03/03/2018 - 09:44

Can I use it as it is
Submitted byDeepak kumar m… (not verified)on Tue, 04/03/2018 - 02:38

Thanks for this code...really strong validation code for password..nice job

Add new comment