Advertisement
hexasoft

Email Validation for Form Submission

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