spacepoet Posted April 8, 2011 Share Posted April 8, 2011 Hi: I have been trying to get this little email validation to work: if(empty($Email) || preg_match('~^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$~',$Email)) { $error .= '- Enter your Email.'; } .... <input type="text" name="Email" size="45" maxlength="50" value="<?php echo $Email; ?>" /> .... If the field is empty, it displays the error message, as it should. However, it does not work when looking for a proper email, like: mysite@mysite.com Can anybody tell me what is wrong? I'm stumped on this ... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/233043-email-validation-does-not-work/ Share on other sites More sharing options...
requinix Posted April 8, 2011 Share Posted April 8, 2011 Look at your logic. if they didn't give an email or it passes the regular expression test, then prompt them for their email address Quote Link to comment https://forums.phpfreaks.com/topic/233043-email-validation-does-not-work/#findComment-1198551 Share on other sites More sharing options...
spacepoet Posted April 8, 2011 Author Share Posted April 8, 2011 I don't follow ... That's where I'm stumped on this ... can you give me some direction ... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/233043-email-validation-does-not-work/#findComment-1198554 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 You're checking if the email field is empty or if it is a valid email. When you give it a valid email the condition will be true. Think about it. Really think about it. Then try this: if(empty($Email) || !preg_match('~^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$~',$Email)) { Quote Link to comment https://forums.phpfreaks.com/topic/233043-email-validation-does-not-work/#findComment-1198559 Share on other sites More sharing options...
spacepoet Posted April 8, 2011 Author Share Posted April 8, 2011 Hi: Thanks - it works great! I see you also have it check for a ".co" extension. I'm not good with RegEx code - never learned much of it. I guess it's time, huh? I'll look at it in better detail, but thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/233043-email-validation-does-not-work/#findComment-1198566 Share on other sites More sharing options...
wright67uk Posted April 29, 2011 Share Posted April 29, 2011 OR perhaps you could use the built in email validator? if($Email == '' or (!filter_var($Email, FILTER_VALIDATE_EMAIL))) { die('<br> You completed the Email field incorrectly, please try again'); } Quote Link to comment https://forums.phpfreaks.com/topic/233043-email-validation-does-not-work/#findComment-1208038 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.