ballouta Posted February 3, 2010 Share Posted February 3, 2010 Hello I have an option to upload a text file and read each line (which should be an email address) the uploading is working fine, but I have problem with the validation function. in the same time i am not sure if the validation function is correct, I googled for several functions but all are giving me the same output that i will describe. validation.php function checkEmail($email){ return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email); } I also tried this function: function checkEmail($email) { if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) return true; else return false; } while the process code is: foreach($text as $line) { //Convert to lowercase first $str = strtolower($line); //Now Validate address if (checkEmail($str)) { //check if this email address already exsist $result = mysql_query("SELECT * FROM `mlist_01` where `email` = '$str' "); $count=mysql_num_rows($result); if($count== 1) { echo "<p dir='ltr'><font color='#FF0000'>This email address $str already exists</font></p>"; } else { //$query="INSERT INTO `mlist_01` (`email` ) VALUES ('$str')"; //$query_result = mysql_query ($query); echo "<p dir='ltr'><font color='#008000'>The email address $str has been inserted</font></p>"; } } else { echo "<p dir='ltr'><font color='#FF0000'>This email address $str is NOT valid</font></p>"; } }//for each loop The output is strange: Only the first line/email appears is green and says it has been inserted: The email address [email protected] has been inserted This email address [email protected] is NOT valid (real test) where all the rest are in red and says invalid emails: This email address [email protected] is NOT valid This email address [email protected] is NOT valid Please Help Many Thanks Link to comment https://forums.phpfreaks.com/topic/190733-validating-emails-function/ Share on other sites More sharing options...
JAY6390 Posted February 3, 2010 Share Posted February 3, 2010 Firstly eregi is soon to be removed from PHP (it's deprecated as of version 5.3.0 and completely removed in PHP6). You should use it's PREG alternate functions, in this case preg_match You could always use filter_var to check the email using the FILTER_VALIDATE_EMAIL filter to check it's a legitimate email pattern as well Link to comment https://forums.phpfreaks.com/topic/190733-validating-emails-function/#findComment-1005859 Share on other sites More sharing options...
ballouta Posted February 3, 2010 Author Share Posted February 3, 2010 Thanks for update info the validation function is now: function checkEmail($email) { if(!filter_var( $email, FILTER_VALIDATE_EMAIL)) return false; else return true; } But still the first email is validated as true and the rest are invalid. Please help Thank you Link to comment https://forums.phpfreaks.com/topic/190733-validating-emails-function/#findComment-1005866 Share on other sites More sharing options...
JAY6390 Posted February 3, 2010 Share Posted February 3, 2010 If they're not validating then it's probably erroneous data being supplied. Make sure you've got no newlines or spaces either side of the email addresses before running them through the check function Link to comment https://forums.phpfreaks.com/topic/190733-validating-emails-function/#findComment-1005870 Share on other sites More sharing options...
ballouta Posted February 3, 2010 Author Share Posted February 3, 2010 tanks for the note but I already checked the text file, each email address on a line and there's no spaces before or after what about my code and the loop that go through the lines? thank you Link to comment https://forums.phpfreaks.com/topic/190733-validating-emails-function/#findComment-1005871 Share on other sites More sharing options...
JAY6390 Posted February 3, 2010 Share Posted February 3, 2010 Yes that could be causing it too. use the strlen function and echo out the email and the strlen of the email and check that they match for each one Link to comment https://forums.phpfreaks.com/topic/190733-validating-emails-function/#findComment-1005873 Share on other sites More sharing options...
JasonLewis Posted February 3, 2010 Share Posted February 3, 2010 Try using trim on the email address before sending it to the checkEmail function, see if it removes any whitespace. Link to comment https://forums.phpfreaks.com/topic/190733-validating-emails-function/#findComment-1005902 Share on other sites More sharing options...
ballouta Posted February 3, 2010 Author Share Posted February 3, 2010 I will check it, and come back to you Thank you Link to comment https://forums.phpfreaks.com/topic/190733-validating-emails-function/#findComment-1005904 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.