Jump to content

Email validation


pras0784

Recommended Posts

Hi,

 

I have done email validation. At present it shows invalid email address if I kept blank but in the same time inserted the records in database. I want user to stay at the same page if anything is invalid.

 

if(!empty($_POST['emailId'])){

if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $_POST['emailId'])){

$records['Email']=$_POST['emailId'];

}

else{

$emsg="Please enter valid Email address";

}}

else{

$emsg="Please enter valid Email address";

}

 

 

And I have used like

 

<tr><td>Email id</td><td><input type="text" name="emailId"></td><td><?php echo ".$emsg." ;?></td></tr>

 

 

Can anybody help me in this regard?

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

You need to check that all of your error messages are empty or not set before inserting into the database, also, you don't need regex to validate an email, you can use filter_var with the filter_validate_email flag. And you would probably benefit from exceptions, unless you wish to have multiple errors display?

 

 

anyway, assuming you want multiple errors:

 

 

$errors  = array();
$records = array();
if ( isset($_POST['submit']) )
{
   // do stuff
   if ( !filter_var($_POST['emailId'], FILTER_VALIDATE_EMAIL) )
      $errors['email'] = 'You must enter a valid email address';
   // validate more stuff
   if ( empty($errors) )
   {
      // insert query
   }
}

 

 

<input name="emailId" value="<?php echo stripslashes(htmlentities($_POST['emailId'], ENT_QUOTES)); ?>" ><?php echo isset($error['email']) ? $error['email'] : ''; ?>

Link to comment
https://forums.phpfreaks.com/topic/256387-email-validation/#findComment-1314417
Share on other sites

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.