How to validate PHP form


Validation of form play an important role while you are working on a live project. Validation is very important for the registration form so that the user get restricted of entering wrong details or from anti programmed.
If any user get succeed in doing anti programming then he may thief your data or this program may crash your database or your site may get hacked. For this we need to restrict the user from entering special character in the input field. So validation of form from unwanted entry is very important, so let's see how to validate form.

At first we will write all the HTML input field, radio button, dropdown list, checkbox etc and a submit button to collect all the filled data.
HTML CODE 
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
<style type="text/css">
table
{
width:50em;
}
table td
{
border:none;
}
fieldset
{
width:20em;
background-color:lightgreen;
}
</style>
</head>
<body bgcolor="lavender">
<div>
<form action="" method="post">
<center />
<fieldset>
<legend><font size="5px" color="blue">Php form validation</font></legend>
<table border="1" cellspacing="10" cellpadding="10">
<tr><td>Name :- </td><td><input type="text" name="name"></td></tr>
<tr><td>Contact number :- </td><td><input type="number" name="contact"></td></tr>
<tr><td>Email :- </td><td><input type="email" name="email"></td></tr>
<tr><td>Password :- </td><td><input type="password" name="password"> </td></tr>
<tr><td>Gender :- </td><td><input type="radio" name="gender">Male <input type="radio" name="gender">female</td></tr>
<tr ><td >D.O.B  </td><td><select name="date">
<option value="date">date</option>
<?php for($i=1; $i<=31; $i++)
{ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>

<select name="month">
<option value="month">month</option>
<?php for($i=1; $i<=12; $i++)
{ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?> </option>
<?php } ?>
</select>

<select name="year">
<option value="year">year</option>
<?php for($i=1980; $i<=2012; $i++)
{ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
</td></tr>
<tr><td>Language Known :- </td><td><input type="checkbox" name="list[]" value="Hindi">Hindi
<input type="checkbox" name="list[]" value="English">English
<input type="checkbox" name="list[]" value="Marathi">Marathi
<input type="checkbox" name="list[]" value="Punjabi">Punjabi
<input type="checkbox" name="list[]" value="Tamil">Tamil
</td></tr>
<tr><td>Nationality :- </td><td><select name="nation">
<option value="-- Select Here --"> -- Select Here -- </option>
<option value="India"> India </option>
<option value="China"> China </option>
<option value="Japan"> Japan </option>
<option value="Australia"> Australia </option>
<option value="Srilanka"> Srilanka </option>
</select>
</td></tr>
<tr><td>Address :- </td><td><textarea rows="5" cols="30" name="address"></textarea></td></tr>
<tr align="center"><td colspan="2"><input type="submit" name="submit" value="submit"></td></tr>
</fieldset>
</form>
</div>
</body>
</html>

Now see below how to validate all this field in php
PHP CODE

  1. <?php
  2. if($_SERVER["REQUEST_METHOD"] == "POST")
  3. {
  4.  $name = check($_POST['name']);
  5.  $email = check($_POST['email']);
  6.  $contact = check($_POST['contact']);
  7.  $password = check($_POST['password']);
  8.  @$gender = $_POST['gender'];
  9.  $date = $_POST['date'];
  10.  $month = $_POST['month'];
  11.  $year = $_POST['year'];
  12.  @$list = implode(",",$_POST['list']);
  13.  $nation = $_POST['nation'];
  14.  $address = check($_POST['address']);

  15. if($year % 4 > 0)
  16. {
  17. if($month == 2)
  18. {
  19. if($date > 28)
  20. {
  21. $error = "please provide correct dob";
  22. }
  23. }
  24. }

  25. if($year % 4 == 0)
  26. {
  27. if($month == 2)
  28. {
  29. if($date > 29)
  30. {
  31. $error = "please provide correct dob";
  32. }
  33. }
  34. }

  35. if($month == 4 || 6 || 9 || 11)
  36. {
  37. if($date > 30)
  38. {
  39. $error = "please provide correct dob";
  40. }
  41. }


  42. if(empty($name))
  43. {
  44. $msg = "Please enter your name";
  45. }
  46. else if(empty($contact))
  47. {
  48. $msg = "please enter valid contact number ";
  49. }
  50. else if(strlen($contact) !== 10)
  51. {
  52. $msg = "please enter valid contact number";
  53. }
  54. else if(empty($email))
  55. {
  56. $msg = "Please enter your email";
  57. }
  58. else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  59. {
  60. $msg = "please enter valid email address";
  61. }
  62. else if(empty($password))
  63. {
  64. $msg = "Please enter your password";
  65. }
  66. else if(strlen($password) <6)
  67. {
  68. $msg = "pssword must not be less than six characters";
  69. }
  70. else if(strlen($password) >15)
  71. {
  72. $msg = "pssword must not be more than fifteen characters";
  73. }
  74. else if(empty($gender))
  75. {
  76. $msg = "Please select your gender";
  77. }
  78. else if($date === 'date')
  79. {
  80. $msg = "please provide dob";
  81. }
  82. else if($month === 'month')
  83. {
  84. $msg = "please provide dob";
  85. }
  86. else if($year === 'year')
  87. {
  88. $msg = "please provide dob";
  89. }
  90. else if(empty($list))
  91. {
  92. $msg = "Please select the language known";
  93. }
  94. else if($nation === '-- Select Here --')
  95. {
  96. $msg = "please select your nationality";
  97. }

  98. else if(empty($address))
  99. {
  100. $msg = "please provide your communicating address";
  101. }
  102. else if(isset($error))
  103. {
  104. echo "<script>alert('$error')</script>";
  105. }
  106. else 
  107. {
  108. echo "<center /><font color=purple size=6px>All the fields are validated correct ";
  109. }
  110. }

  111. ?>
  112. <?php
  113. function check($data)
  114. {
  115. $data = trim($data);
  116. $data = stripslashes($data);
  117. $data = htmlspecialchars($data);
  118. return $data;
  119. }
  120. if(isset($msg))
  121. {

  122. echo "<script>alert('$msg')</script>";
  123. }
  124. ?>


Explanation :-
Line 4 to 14         :- Storing the form data value in variables and checking for special character enter in the text field by the users or by hackers to avoid from anti programming or data thief  by creating a check function.
Line 12                :- Used of implode function to provide comma between the two array value.
Line 16 to 44       :- Validating Date from entering wrong date of birth. And checking for leap year.
Line 47 to 107     :- Checking all the fields are be filled by the user or not.
Line 119 to 132   :- Working functionality or check function and displaying error message.

No comments: