Jump to content

Email Structure Reader?


justlukeyou

Recommended Posts

Hi,

 

I'm trying to use a script which reads the structure of email such as '[email protected]'.

 

My code currently accepts 'anything'.

 

I've tried using this:

 

http://www.w3schools.com/php/php_secure_mail.asp

 

And the following code. Is there a preferred way of reading and varifying an email?

 

  <div class="registerinputright"> 
	    <input class="field" type="text" name="email"  type="text" width="600" value="<?php echo htmlspecialchars($registerEmail); ?>" /> 
	    <?php if($errors['registerEmail']) print '<div class="invalid">' . $errors['registerEmail'] . '</div>'; ?> 

Link to comment
https://forums.phpfreaks.com/topic/270921-email-structure-reader/
Share on other sites

1. Do not use w3schools as a resource. Their material for the most part is outdated and misleading.

 

Typically for email validation I like using the filter_var function with the FILTER_VALIDATE_EMAIL flag set.

 

Ironically, on the link he provided that W3S page also uses filter_var().

Thanks,

 

Whats the part on here which filters emails http://uk3.php.net/filter_var

 

Should it be something like this? UserEmail is the email the user enters.

 

 <?php filter_var('userEmail', (FILTER_VALIDATE_EMAIL)); ?>

 

I still struggle understand a word of the PHP site but now Im just working on the details of membership script. The structure is there.

I found this which is much easier to follow. But how do I change "[email protected]" to the actual email that is being inserted?

 

<?php
$email = "[email protected]";
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
 {
 echo "E-mail is not valid";
 }
else
 {
 echo "E-mail is valid";
 }
?>

Right, sorted it. However I have to errors. One if the email box is blank and one if the email address is not a valid structure.

 

How do I make it so that if the email box is blank only the first error appears?

 

 if(!isset($registerEmail) || empty($registerEmail)) {
    $errors['registerEmail1'] = "Please enter your email address.";
   }

$email = "$registerEmail";
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
 {
    $errors['registerEmail'] = "Please enter your email address in a valid format.  Example: [email protected]";
 }
else
 {
    $errors['registerEmail'] = "E-mail is valid";
 }

There are a couple of ways, but the most basic would be to change the second if to an elseif (also, your making life harder for yourself the way you are assigning $email, have a look):

if(!isset($registerEmail) || empty(trim($registerEmail))) {
$errors['registerEmail1'] = "Please enter your email address.";
}
elseif(!filter_var($regiserEmail, FILTER_VALIDATE_EMAIL) {
$errors['registerEmail'] = "Please enter your email address in a valid format. Example: [email protected]";
}
else {
$errors['registerEmail'] = "E-mail is valid";
}

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.