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.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
bool3max
  • 2,748
  • 5
  • 28
  • 57
  • 2
    Note that there are many valid email addresses that will fail your validation I.e. `foo+bar@example.com` or 'foo@example.info`. – Jim Aug 10 '15 at 12:17
  • 2
    That regex will block many valid email addresses: including those of internationalised top level domains. – Richard Aug 10 '15 at 12:18
  • Thanks for the info. I'll rewrite the pattern soon. – bool3max Aug 10 '15 at 12:20
  • 1
    Don't forget that there can be domains with more letters, e.g. `info@me.ninja`. And also, I am not sure, but shouldn't it be `[A-z]`instead of `[a-Z]`? – Haudegen Aug 10 '15 at 12:22
  • This question has been asked already before, see http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – David De Sloovere Aug 10 '15 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? – TeeDeJee Aug 10 '15 at 12:23
  • I forgot that there's a type="email" attribute in HTML5. I'll use that. But it's definitely interesting to tinker around with regex. – bool3max Aug 10 '15 at 12:29
  • 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"` – Haudegen Aug 10 '15 at 12:30

2 Answers2

1

preg_match will do what you need.

if(preg_match($regex,$email)){
    echo "Email matches regex!";
}
Jim
  • 22,354
  • 6
  • 52
  • 80
  • And... the answer that was posted 4 minutes ago is accepted, not this one from 16 mins ago. Because that makes sense. – Darragh Enright Aug 10 '15 at 12:37
  • 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. – bool3max Aug 10 '15 at 12:46
1

Based on your regular expression:

$email = "test123@email.com";
$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.

Manu
  • 185
  • 4
  • 11
  • Thanks. Just what I was looking for. Also, note that my regex pattern is incorrect as it will not accept most valid email addresses. – bool3max Aug 10 '15 at 12:34
  • 1
    Try it with: `test123@email.ninja`, `test123@email.info`, `test123@email.travel` and`test123@email.history`. All of them are valid eMail Adresses ... – Haudegen Aug 10 '15 at 12:36
  • 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. – bool3max Aug 10 '15 at 12:45
  • 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! – Haudegen Aug 10 '15 at 12:48
  • I don't get why people down-vote my answer, I offered a valid answer according to aCodingN00b's question... – Manu Aug 11 '15 at 11:56