Jump to content

Adding email validation


Russia

Recommended Posts

I would also like to add a email validation to it. How would I go about doing that without messing up the coding?

 

So basically even if the whole form is filled out, and the email is in the wrong format, then do the same thing as when the form isn't totally filled out. If the email is in the correct format but the whole form isnt filled out, then show an error message, I already have all that coded in, exept the email validation.

 

Can someone add that to my code?

 

<?php
  $form = '<form action="" method="POST">   
Email Address:  
<input size="40" type="text" value="' . $_POST["email_address"] . '" name="email_address"><br>  
Username  
<input size="40" type="text" value="' . $_POST["username"] . '" name="username"><br>  
password  
<input size="40" value="' . $_POST["password"] . '" type="text" name="password"><br>  
<input id="submit" class="submit-button" type="submit" name="submit" value="Reset Password" >  
</form>';
  if (isset($_POST['submit'])) {
      $blank_inputs = 0;
      $test_array = array_count_values($_POST);
      $blank_inputs = $test_array[""];
      if ($blank_inputs > 0) {
          //display an error message and put the form back up for editing 
          echo "Please fill out the whole form.<hr>";
          echo $form;
      } else {
          echo 'message sent';
      }
  } else {
      echo $form;
  }
?> 

 

 

Link to comment
https://forums.phpfreaks.com/topic/184708-adding-email-validation/
Share on other sites

      if (($blank_inputs > 0) || (preg_match("/^[a-z0-9._-]+?@[a-z0-9-]+?(?:\.[a-z]{1,4}){1,2}$/i",$_POST['email']))) {
          //display an error message and put the form back up for editing 
          echo "Please fill out the whole form.<hr>";
          echo $form;
      } else {
           echo 'message sent';
      }

So something like this?

 

<?php
  $email = $_POST['email_address'];
  $form = '<form action="" method="POST">   
Email Address:  
<input size="40" type="text" value="' . $_POST["email_address"] . '" name="email_address"><br>  
Username  
<input size="40" type="text" value="' . $_POST["username"] . '" name="username"><br>  
password  
<input size="40" value="' . $_POST["password"] . '" type="text" name="password"><br>  
<input id="submit" class="submit-button" type="submit" name="submit" value="Reset Password" >  
</form>';
  if (isset($_POST['submit'])) {
      $blank_inputs = 0;
      $test_array = array_count_values($_POST);
      $blank_inputs = $test_array[""];
      if (($blank_inputs > 0) || (preg_match("/^[a-z0-9._-]+?@[a-z0-9-]+?(?:\.[a-z]{1,4}){1,2}$/i",$_POST['email']))) {
          //display an error message and put the form back up for editing 
          echo "Please fill out the whole form.<hr>";
          echo $form;
      } else {
          echo 'message sent';
      }
  } else {
      echo $form;
  }
?> 

 

Also, what does || mean in php?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.