-4
^([a-z0-9_\.-])+@[yahoo]{5}\.([com]{3}\.)?[com]{3}$

this currently matches xxxx@yahoo.com , how can I rewrite this to match some additional domains? for example, gmail.com and deadforce.com. I tried the following but it did not work, what am I doing wrong?

^([a-z0-9_\.-])+@[yahoo|gmail|deadforce]{5,9}\.([com]{3}\.)?[com]{3}$

Thank you in advance!

Bobby Bruce
  • 341
  • 5
  • 12
  • 1
    There are tons of answers about validating email addresses with regex. – Anonymous Jun 17 '15 at 15:32
  • 2
    You flagged the question both "javascript" and "php" - which is it? Also see http://www.regular-expressions.info – cxw Jun 17 '15 at 15:33
  • possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Rick Smith Jun 17 '15 at 16:03

2 Answers2

0

Email validation is a notoriously difficult problem, and many people have failed quite horribly at trying to validate them themselves.

Filter var has a filter just for emails. Use that to check for email address validity. See http://php.net/manual/en/function.filter-var.php

if (filter_var('bob@example.com', FILTER_VALIDATE_EMAIL)) {
    // Email is valid
}

There's probably no downside to doing the domain check the easy way. Just check for the domain strings in the email address. e.g.

if (
    filter_var($email, FILTER_VALIDATE_EMAIL) &&
    preg_match("/@(yahoo|gmail|deadforce)\.com/", $email)
) {
    // Email is valid
}

In terms of your original regular expression, quite a lot of it was incorrect, which is why you were having trouble changing it. regexper shows what you've created.

([a-z0-9_\.-])+ should be [a-z0-9_\.-]+ or ([a-z0-9_\.-]+)

The () are only capturing results in this section. If you want results move the brackets, if not remove them.

[yahoo]{5} should be yahoo

That's matching 5 characters that are one of y,a,h,o so it would match hayoo etc.

\.([com]{3}\.)?[com]{3} should be \.com

Dunno what this was trying to accomplish but you only wanted .com

Take a look at http:// www.regular-expressions.info /tutorial.html for a guide to regular expressions

Tom Boothman
  • 146
  • 6
0

Your regex doesn't say what you think it says.

^([a-z0-9_\.-])+@[yahoo]{5}\.([com]{3}\.)?[com]{3}$

Says any characters a-z, 0-9, ., - one or more times.

That later part where you are trying match yahoo.com is incorrect. It says y, a, h, or o, any of those characters are allowed 5 times. Same with the com so aaaaa.ooo would be valid here. I'm not sure what the ([com]{3}\.)?[com]{3} was trying to say but I presume you wanted to check for .com.

See character classes documentation here, http://www.regular-expressions.info/charclass.html.

What you want is

^([a-z0-9_.\-])+@yahoo\.com$

or for more domains use grouping,

^([a-z0-9_.\-])+@(yahoo|gmail|deadforce)\.com$

You haven't stated what language you are using so a real demo can't be given.

Functional demo, https://jsfiddle.net/qa9x9hua/1/

chris85
  • 23,846
  • 7
  • 34
  • 51