2

Basically I have a little form on my site, and when I submit it, I want php to check if the email input <input type="text" name="destinationMail"> is matching the correct email regex pattern, which is : [a-Z0-9.]+[@][a-Z]+[.][a-Z]{3}

And I know that there are functions in PHP that check for a correct email (I think), but I'd like to use a regular expression for this.

Thanks.

8
  • 2
    Note that there are many valid email addresses that will fail your validation I.e. [email protected] or '[email protected]`. Commented Aug 10, 2015 at 12:17
  • 2
    That regex will block many valid email addresses: including those of internationalised top level domains. Commented Aug 10, 2015 at 12:18
  • 1
    Don't forget that there can be domains with more letters, e.g. [email protected]. And also, I am not sure, but shouldn't it be [A-z]instead of [a-Z]? Commented Aug 10, 2015 at 12:22
  • 1
    If you know there's a function for it. Why would you want to make a regex with the possibility to exclude valid email addresses? Commented Aug 10, 2015 at 12:23
  • 1
    Use it, but it is not enough. Always double check! If its possible to check in PHP, check it again, because the frontend can be tricked really easy. (PHP could be tricked, too, but it isn't that easy anymore!) And also, it's not supported by all Browsers. Other browsers will treat it as type="text" Commented Aug 10, 2015 at 12:30

2 Answers 2

1

Based on your regular expression:

$email = "[email protected]";
$regex = "^[A-z0-9.]+@[A-z]+\.[A-z]{3}+$^";

if (preg_match($regex, $email)) 
{
    echo $email . " = valid.";
} else 
{ 
    echo $email . " = invalid";
} 

Although I would suggest php filter_var which is safer and better than your regular expression.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Just what I was looking for. Also, note that my regex pattern is incorrect as it will not accept most valid email addresses.
Try it with: [email protected], [email protected], [email protected] and[email protected]. All of them are valid eMail Adresses ...
I know, I'm writing a new one now. Although I think I'll stick with the type="email" for now. The only bad thing about it is that users can just edit the type in the browser's development tools and, (I think), get away with a non-valid email address.
Yeah, that could happen, and it's not that hard, two minutes of googleing. But condsider the fact, that, like many others already told you, you will block many valid eMail adresses (with this regex), so maybe its even better not to use it, rather then use it. The rest is upon you!
I don't get why people down-vote my answer, I offered a valid answer according to aCodingN00b's question...
1

preg_match will do what you need.

if(preg_match($regex,$email)){
    echo "Email matches regex!";
}

2 Comments

And... the answer that was posted 4 minutes ago is accepted, not this one from 16 mins ago. Because that makes sense.
Umm Manos's answer was just more clear and it had a better example. This answer was just as good. I can't mark both as answered though lel.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.