phpcodec Posted August 16, 2008 Share Posted August 16, 2008 I cant figure out how to write a regex that will detect valid email address, can someone help me please? Quote Link to comment https://forums.phpfreaks.com/topic/119952-email-regex/ Share on other sites More sharing options...
nrg_alpha Posted August 16, 2008 Share Posted August 16, 2008 When it comes to things like this.. I prefer to use: $email = filter_var(filter_var($_POST['email'], FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL); if($email === true){ // Since condition is true, do this... } else { // Otherwise it is false, do that... } The idea is passing your $_POST variable through PHP's built in sanitiser and validation system and assigning it to the variable $email. One issue with building your own custom expressions for such tasks is that if it is built too strict (isn't flexible enough), you may be blocking out certain email adressess.. Case in point..suppose the back end of your expression only matches for either '.xx' (which could represent .ca for instance) -or- '.xxx' (to accomodate .com for instance), or .xx.xx (think .co.uk).. well, what happens when someone sends an email with '.asia' ? From what I'm gathering, it is better to be 'loose fisted' as opposed to 'tight fisted'. So these built in functions stays *somewhat* true to email formats, yet is forgiving to some extent (which is recommended for the sake of allowing more emails that would otherwise not make it through). Just a thought. Cheers, NRG EDIT: I am also going to hazzard a guess that this system allows for future domain extended formats that may arise without the need to retool anything. Quote Link to comment https://forums.phpfreaks.com/topic/119952-email-regex/#findComment-618079 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.