worldcomingtoanend Posted October 22, 2009 Share Posted October 22, 2009 in my php mail script i tried using the first code below and for some reason it keeps giving me "invalid email entered" i hv changed it to the second code below and i still get the same error. surprisingly the code works on my personal pc but on my live server it gives me those errors. where could i be going wrong? thank u for your help. function is_valid_email($email) { return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email); } if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif ($subject == "") { echo "<h4>No subject</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } Link to comment https://forums.phpfreaks.com/topic/178584-whats-wrong-with-my-email-validation-preg_match/ Share on other sites More sharing options...
Bricktop Posted October 22, 2009 Share Posted October 22, 2009 Hi worldcomingtoanend, I always use this eregi() check if requiring email validation and it seems to work very well: (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) Hope this helps. Link to comment https://forums.phpfreaks.com/topic/178584-whats-wrong-with-my-email-validation-preg_match/#findComment-941851 Share on other sites More sharing options...
purpleshadez Posted October 22, 2009 Share Posted October 22, 2009 Only issue with eregi is that it is depreciated as of PHP 5.3.0. with ereg I used this and never had any issues: <?php function check_email($email) { // First, we check that there's one @ symbol, and that the lengths are right if ( ! ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. return FALSE; } // Split it into sections to make life easier $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) { if ( ! ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { return FALSE; } } if ( ! ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) { return FALSE; // Not enough parts to domain } for ($i = 0; $i < sizeof($domain_array); $i++) { if ( ! ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { return FALSE; } } } return TRUE; } // End check_email?> To be honest though, using the built in filter_var() function is a good idea if you're on php 5.3.0 <?php if(filter_var($email, FILTER_VALIDATE_EMAIL) === TRUE) { // valid } else { // not valid } Link to comment https://forums.phpfreaks.com/topic/178584-whats-wrong-with-my-email-validation-preg_match/#findComment-941856 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.