1

I don't mess around with Regex too much but have been able to get this one online. /.+@.+/. This will return true with both joe@joe and joe@joe.com. I want to make it so a user must supply a domain extension otherwise I want it to fail, I presume this is quite simple but I just can't figure it out. I've tried /.+@.+.\S/ but that didn't work. Any help would be great, thanks!

This will be used in both PHP and javascript. The current one works in both, the new will need to also.

Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
  • 3
    Possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – cartant Mar 28 '17 at 04:26
  • 1
    From http://www.w3.org/TR/html5/forms.html#valid-e-mail-address `^[a-zA-Z0-9.!#$%&'*+/=?^_\`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$` –  Mar 28 '17 at 04:49

5 Answers5

2

Here is expression

/\w+@\w+\.\w{2,10}/

to allow more characters:

/[\w\-\._]+@[\w\-\._]+\.\w{2,10}/
diavolic
  • 722
  • 1
  • 4
  • 5
1

The regex here works for me (from http://www.regextester.com/19 )

/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/i

As does this example of regex inside JavaScript from plnkr here: http://embed.plnkr.co/ZlbA1I2TsDBUmDb9o0gj/

David
  • 3,285
  • 1
  • 37
  • 54
0

Given that I don't know what the rest of your code is and you might really need this for both PHP and JavaScript I will suggest a different approach as I don't agree with the solution given in the accepted answer because it will match email addresses like -@-.aa, .@.-.aa, .@_.aa etc.

I'd suggest you use PHP's filter_var with the FILTER_VALIDATE_EMAIL filter which

validates e-mail addresses against the syntax in RFC 822, with the exceptions that comments and whitespace folding and dotless domain names are not supported.

and probably an additional AJAX call from JavaScript .

Regular Expressions matching all valid email address are not trivial at all and I'm not even sure you can rely on them for matching ALL valid email addresses.

For more information please take a look at Validate email address in JavaScript? and Using a regular expression to validate an email address

Community
  • 1
  • 1
mrun
  • 744
  • 6
  • 19
  • 24
-1

.+\@.+\.{1}.+

This is a simple regex and will match the required criteria

Mayur Padshala
  • 2,037
  • 2
  • 19
  • 19
-1

For a simple regex that will accept any domain extension of one character or longer, try: /.+@.+\..+/

For a 2 character domain extension or longer, try: /.+@.+\..{2,}/

D_S_B
  • 208
  • 3
  • 9