Jump to content

[SOLVED] Function is not working


EchoFool

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

^[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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.