tags:

views:

57

answers:

4

Just wondering why this is too strict, I can send very simplified emails to say [email protected] or [email protected]

but if I make the email any longer ([email protected]) it does not get sent.

Instead it echos back my error message:Invalid Email Address Supplied

    // Create a function to check email 
function checkEmail($email)
{
// Add some regex 
  return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
}
A: 

Hi,

please try

'/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/'

it also fits for subdomain email adresses as [email protected]

tuergeist
+2  A: 

This part

@[\w\d.-]{2,}

is gobbling up

@gmail.com

leaving nothing for this part

[\w\d.-]{2,}

to match.

Better to reuse something already proven, see for example http://www.regular-expressions.info/email.html

Ed Guiness
Thanks for the link, Just wondering does Ctrl+F on your keyboard use some type of regex to find stuff, I think so, it even has the option to Match a case, but they don't call it regex, just Find?
Newb
Huh? The behaviour of Ctrl+F depends entirely on which application receives those keys when I hit them.
Ed Guiness
I just meant the way it uses certain matches, even if it's a web browser, word doc, command prompt, shell console, or notepad++, or just grepping.
Newb
Isn't the objective usually to match a given case and do something with the matched data right?Block or allow certain emails or replace certain words, rename directories, etc?
Newb
@Newb - OK let me take wild guess at what you're asking. You're asking if all FIND operations use RegEx? The answer is no. Why should they? If I want to search for string "hola" in a file containing "abc hold hole hulk holabc" I can just look for the string itself, no Regex magic required. RegEx allows you (the developer) to search for a variety of strings using one expression (the regex) instead of many expressions (literal strings). Is that what you're asking about?
Ed Guiness
Thanks for the info edg, yes, that's what I was asking.
Newb
+1  A: 

I usually don't fret myself too much for checking email validity. I just need to check there is a value in front of "@" and at the back. That's all. The rest of the "checking" job, the MTAs will do that for me. If its invalid email, i will get a response from MTA. If its able to be sent out, that means the email is most probably valid.

ghostdog74
+3  A: 

If you have access to php 5.2 or above, you should use the filter functions :

function checkEmail($email){
  return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

Or validate it, "the right way".

Arkh