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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.