EchoFool Posted May 25, 2008 Share Posted May 25, 2008 I have a function to check an email validity, but it just always returns false even if I input a valid email :S This is what i have: <?php //function function checkEmail($Email) { if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $Email)) { return TRUE; }Else{ return FALSE; } } //end function //processing $Email = mysql_real_escape_string($_POST['Email']); If(strlen($Email) > 50){ Header("location: Login.php?register&EmailError"); Die; } if(checkEmail($Email) == FALSE){ //this is where it headers out every time Header("location: Login.php?register&EmailError"); Die; } ?> Where did I go wrong ? Hope you can help! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/ Share on other sites More sharing options...
awpti Posted May 25, 2008 Share Posted May 25, 2008 No need to escape a real email address. Don't bother - it won't validate if it needs to be escaped It's escaping the @ (someone\@somedomain.com) and making it invalid. Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549590 Share on other sites More sharing options...
EchoFool Posted May 25, 2008 Author Share Posted May 25, 2008 So i should removed the mysql_real_escape_string from it ? But what if some one puts the email: '"@test.com ? Won't the ' or " crash it ? Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549592 Share on other sites More sharing options...
wildteen88 Posted May 25, 2008 Share Posted May 25, 2008 No need to escape a real email address. Don't bother - it won't validate if it needs to be escaped It's escaping the @ (someone\@somedomain.com) and making it invalid. mysql_real_escape_string wont affect the @ symbol. Does the email address validate if you don't run mysql_real_escape_string Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549593 Share on other sites More sharing options...
.josh Posted May 25, 2008 Share Posted May 25, 2008 No, because your eregi won't validate those characters. Or at least, it shouldn't. I'm not an expert at regex patterns so I don't know if your pattern will or will not validate those characters, but it shouldn't. In unrelated news, seeing as how you're going through the trouble to make a function to validate the email, I would suggest putting your strlen check inside your function, as well. Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549597 Share on other sites More sharing options...
EchoFool Posted May 25, 2008 Author Share Posted May 25, 2008 No need to escape a real email address. Don't bother - it won't validate if it needs to be escaped It's escaping the @ (someone\@somedomain.com) and making it invalid. mysql_real_escape_string wont affect the @ symbol. Does the email address validate if you don't run mysql_real_escape_string Hmm no it doesn't it still returns false Crayon Violent - Good point, I shall put it in my function once I have solved this email validation, thankyou. What other options have I got to get this email working, what would you do to validate email ? Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549599 Share on other sites More sharing options...
wildteen88 Posted May 25, 2008 Share Posted May 25, 2008 No need to escape a real email address. Don't bother - it won't validate if it needs to be escaped It's escaping the @ (someone\@somedomain.com) and making it invalid. mysql_real_escape_string wont affect the @ symbol. Does the email address validate if you don't run mysql_real_escape_string Hmm no it doesn't it still returns false Then there must be a problem with your regular expression if a valid email address format is not passing your checks. Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549600 Share on other sites More sharing options...
EchoFool Posted May 25, 2008 Author Share Posted May 25, 2008 I don't follow.. I got this function from a tutorial on how to validate emails... This was where I had got it from: http://www.spoono.com/php/tutorials/tutorial.php?id=41 I just didn't do the latter check which is to check the domain was real or not. As you can see I am still quite new to this type of validation. Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549608 Share on other sites More sharing options...
deadonarrival Posted May 25, 2008 Share Posted May 25, 2008 ^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$] I believe the problem may lie with your final ] Your regex, broken down, is ^ start of line [a-zA-Z0-9_]+ one or more of numbers, letters or _ @ literal @ char [a-zA-Z0-9\-]+ one or more of numbers, letters - or . \. literal . char [a-zA-Z0-9\-\.]+ one or more of letters, numbers $ end of line ] and a random square bracket The rest of it SHOULD validate, although you could do with more options in your repetitions, but the bracket is what I think is breaking it. Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549621 Share on other sites More sharing options...
EchoFool Posted May 25, 2008 Author Share Posted May 25, 2008 Good spot! Works now ! Quote Link to comment https://forums.phpfreaks.com/topic/107197-solved-function-is-not-working/#findComment-549734 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.