Jump to content

Recommended Posts

Hi,

 

I'm trying to use a script which reads the structure of email such as 'something@website.com'.

 

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 "someone@example.com" to the actual email that is being inserted?

 

<?php
$email = "someone@example.com";
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: bobsmith@companyname.com";
 }
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: bobsmith@companyname.com";
}
else {
$errors['registerEmail'] = "E-mail is valid";
}

Edited by Muddy_Funster
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.