justlukeyou Posted November 19, 2012 Share Posted November 19, 2012 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 More sharing options...
AyKay47 Posted November 20, 2012 Share Posted November 20, 2012 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. Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1393685 Share on other sites More sharing options...
Psycho Posted November 20, 2012 Share Posted November 20, 2012 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(). Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1393690 Share on other sites More sharing options...
AyKay47 Posted November 20, 2012 Share Posted November 20, 2012 Oh the irony! Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1393769 Share on other sites More sharing options...
justlukeyou Posted November 22, 2012 Author Share Posted November 22, 2012 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. Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1394511 Share on other sites More sharing options...
justlukeyou Posted November 22, 2012 Author Share Posted November 22, 2012 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1394513 Share on other sites More sharing options...
justlukeyou Posted November 22, 2012 Author Share Posted November 22, 2012 Sorted, thanks guys for pointing me in the right direction. <?php $email = "$registerEmail"; if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "E-mail is not valid"; } else { echo "E-mail is valid"; } ?> Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1394514 Share on other sites More sharing options...
justlukeyou Posted November 22, 2012 Author Share Posted November 22, 2012 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"; } Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1394517 Share on other sites More sharing options...
Muddy_Funster Posted November 23, 2012 Share Posted November 23, 2012 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"; } Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1394570 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.