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 '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>'; ?> Quote 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. Quote 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(). Quote 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! Quote 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. Quote 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 "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"; } ?> Quote 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"; } ?> Quote 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: bobsmith@companyname.com"; } else { $errors['registerEmail'] = "E-mail is valid"; } Quote 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 (edited) 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 November 23, 2012 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/270921-email-structure-reader/#findComment-1394570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.