0

I am using the following expression to validate emails:

^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+(?:[a-zA-Z]{2}|com|COM|CoM|cOM|coM|COm|edu|org|net|gov|biz|info|name|aero|biz|info|jobs|museum)\b

However, it still allows me to enter an invalid email such as below...

mynameis@domain.co,uk

I want to not allow the comma between the co and uk.

Any suggestions?

Stephan
  • 41,764
  • 65
  • 238
  • 329
Steve
  • 1
  • 1

1 Answers1

3

If you do not want commas anywhere, you could try to lookahead. ^(.(?!,))*$ With this, if you have a comma anywhere, it will no longer match. Of course you'll need to place this in your existing expression, without the start and end anchors. I recommend Regexpal to test such regular expressions.

Additionally, you may wish to exclude double periods from your email address. I use:

^(?!.*\.{2})[ ]*[\w]+[\w\.]*[\w]*@[\w]+\.[a-zA-Z]+[ ]* for my needs.

Now \w allows for [A-Za-z0-9_] which is odd due to the underscore but I'm pretty sure underscores are allowed in email addresses. This also prevents a double period in your email address as well as not allowing the first or last character to be a period, which is illegal.

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
  • Yes, I don't want to allow commas anywhere as this is only for a single email address, so no need to add a comma separator for multiple email addresses. – Steve Jun 28 '12 at 08:22
  • This one seems to work... ^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$ – Steve Jun 28 '12 at 09:53
  • That might work, however be careful as you cannot have two consecutive periods in a row with your email address. I'll update my answer with a solution for that. – BlackVegetable Jun 28 '12 at 14:05