Email Validation For Form Submission


/ Published in: PHP
Save to your folder(s)

This code will demonstrate how to implement the email validation for form submission by using php.
You can obtain a free account at https://www.mailboxvalidator.com/pay/9


Copy this code and paste it in your HTML
  1. <?php
  2. /********************************************************************************/
  3. /*Description: This code will demonstrate how to implement the email validation for form submission by using php. */
  4. /* For information, please visit https://www.mailboxvalidator.com/ */
  5. /*******************************************************************************/
  6. /* You can obtain a free account at https://www.mailboxvalidator.com/pay/9 */
  7. ?>
  8.  
  9. <html>
  10. <head>
  11. <title>Sign Up Page</title>
  12. <meta name="viewport" content="width=device-width, initial-scale=1">
  13. <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
  14. <link rel="stylesheet" href="css/bootstrap-theme.min.css">
  15. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  16. <link rel="stylesheet" type="text/css" href="login_form.css">
  17. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  18. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  19. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  20. <script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.5/validator.min.js"></script>
  21. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"></link>
  22. </head>
  23. <body>
  24. <div class="login">
  25. <div class="login_container">
  26. <div class="login_panel panel panel-default">
  27. <div class="panel-body">
  28. <h1 id="textHead" class="text-center">Sign Up</h1>
  29. <hr>
  30. <form data-toggle="validator" name="login_form" id="textContent" class="form-horizontal" method="post" action="su.php">
  31. <div class="form-group">
  32. <label class="control-label col-sm-3"></label>
  33. <div class="col-sm-12">
  34. <h4>Email:</h4>
  35. <input type="text" data-error="Please enter your email field" class="form-control" name="email" placeholder="Enter email" id="email" required>
  36. <!--Check whether the field is empty-->
  37. <div class="help-block with-errors"></div>
  38. </div>
  39. </div>
  40. <p id="email"></p>
  41. <div class="form-group">
  42. <label class="control-label col-sm-3"></label>
  43. <div class="col-sm-12">
  44. <h4>Password:</h4>
  45. <!--Set the minimum length of password-->
  46. <input type="password" data-error="Please enter minimum of 6 characters." data-minlength="6" class="form-control" name="password" placeholder="Enter password" id="password" required>
  47. <!--Check whether the field is empty-->
  48. <div class="help-block with-errors"></div>
  49. </div>
  50. </div>
  51. <div class="form-group">
  52. <label class="control-label col-sm-3"></label>
  53. <div class="col-sm-12">
  54. <h4>Confirm Password:</h4>
  55. <input type="password" data-error="Please reenter password" class="form-control" name="password" placeholder="Enter password" id="password" data-match="#password" data-match-error="Password does not match" required>
  56. <!--Check whether the field is empty-->
  57. <div class="help-block with-errors"></div>
  58. </div>
  59. </div>
  60. <p id="password"></p>
  61. <div class="form-group">
  62. <div class="col-sm-12">
  63. <input type="Submit" class="btn btn-block btn-default btn-info" value="Sign Up">
  64. </div>
  65. </div>
  66. </form>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. </body>
  72. </html>
  73.  
  74.  
  75. <?php
  76. $servername = "localhost";
  77. $email = "root";
  78. $password = "";
  79. $database = "";
  80.  
  81. // Create connection to database
  82. $conn = new mysqli($servername, $email, $password,$database);
  83.  
  84. $apiKey = 'Enter your API key';
  85. $query = '';
  86.  
  87. if(!empty($_POST['email']) && !empty($_POST['password']))
  88. {
  89. $email = $_POST['email'];
  90. $pass = $_POST['password'];
  91. $params['email']= $email;
  92.  
  93. //Validate the email through MailboxValidator
  94. foreach($params as $key=>$value)
  95. {
  96. $query .= '&' . $key . '=' . rawurlencode($value);
  97. }
  98. $try = 0;
  99. do
  100. {
  101. ////////////
  102. //For https request, please make sure you have enabled php_openssl.dll extension.
  103. //
  104. //How to enable https
  105. //- Uncomment ;extension=php_openssl.dll by removing the semicolon in your php.ini, and restart the apache.
  106. //
  107. //In case you have difficulty to modify the php.ini, you can always make the http request instead of https.
  108. ////////////
  109.  
  110. $result = file_get_contents('https://api.mailboxvalidator.com/v1/validation/single?key=' . $apiKey . $query);
  111. }while(!$result && $try++ < 3);
  112.  
  113. $data = json_decode($result);
  114.  
  115. //Check if query is success
  116. if ($data->is_syntax=='True')
  117. {
  118. //Get the data from database
  119. $query_email = "SELECT email FROM signup WHERE email='$email'";
  120. $query_run_email = mysqli_query($conn,$query_email);
  121.  
  122. //Check the input with database
  123. if (mysqli_num_rows($query_run_email))
  124. {
  125. header( "refresh:0; url= signup.php" );
  126. echo '<script language="javascript">';
  127. echo 'alert("Email already exists")';
  128. echo '</script>';
  129. }
  130.  
  131. else
  132. {
  133. //Insert data into database
  134. $sql = "INSERT INTO signup(email,password)VALUES ('$email', '$pass')";
  135. mysqli_query($conn, $sql);
  136. //The message of account create successful
  137. header( "refresh:0; url= signup.php" );
  138. echo '<script language="javascript">';
  139. echo 'alert("Account created successful")';
  140. echo '</script>';
  141. }
  142. }
  143.  
  144. else
  145. {
  146. //Error message of validation
  147. header( "refresh:0; url= signup.php" );
  148. echo '<script language="javascript">';
  149. echo 'alert("Account created failed, please enter a valid email!")';
  150. echo '</script>';
  151. }
  152. /* if need more information below is the example of error message
  153. else if ($data->is_free=='True')
  154. {
  155. header( "refresh:0; url= signup.php" );
  156. echo '<script language="javascript">';
  157. echo 'alert("Account created failed, free email are not supported!")';
  158. echo '</script>';
  159. }
  160. else if ($data->is_domain=='False')
  161. {
  162. header( "refresh:0; url= signup.php" );
  163. echo '<script language="javascript">';
  164. echo 'alert("Account created failed, please make sure your domain name is correct.")';
  165. echo '</script>';
  166. }
  167. else if ($data->is_suppressed == 'True')
  168. {
  169. header( "refresh:0; url= signup.php" );
  170. echo '<script language="javascript">';
  171. echo 'alert("Account created failed, your email address has been blacklisted!")';
  172. echo '</script>';
  173. }
  174. else if ($data->is_verified == 'False')
  175. {
  176. header( "refresh:0; url= signup.php" );
  177. echo '<script language="javascript">';
  178. echo 'alert("Account created failed, please enter a verified email!")';
  179. echo '</script>';
  180. }
  181. */
  182. }
  183.  
  184. ?>

URL: https://www.mailboxvalidator.com/resources/articles/email-validation-for-form-submission/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.