0

I am trying to validate an email using the regular expressions. I alomost done but I am failing in one condtion alone which is "a '-' (hyphen) should not be followed by '@'". I tried in different ways but nothis worked. Below is the regex I am currently using.

regex = /^(?!.*\.{2})[a-zA-Z0-9][a-zA-Z0-9#$%&\*\+-/=\?\_`|~]*@[a-zA-Z0-9][a-zA-Z0-9-_.]*\.[a-zA-Z]{2,4}$/;

PS : I know the the above regex stops for using two consecutive periods but its my project requirement :(

Please help me in validating for '-' followed by '@' and vice-versa.

Thanks, Yeshwanth

Engineer
  • 47,849
  • 12
  • 88
  • 91
Yeshwanth Kota
  • 481
  • 1
  • 5
  • 10
  • You can pick a complete regex in http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – Arcadien Jul 06 '12 at 11:02

2 Answers2

1

Easiest is to add (?!.*-@) after your first lookahead.

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
0

Why not just add

[a-zA-Z0-9#$%&\*\+-/=\?\_`|~]*[a-zA-Z0-9#$%&\*\+/=\?\_`|~]@

instead of

[a-zA-Z0-9#$%&\*\+-/=\?\_`|~]*@

?

And is it OK to have a + or, say, a # right before the dog?

Qnan
  • 3,714
  • 18
  • 15
  • Thanks for your suggestion. This works too :).. Yes, as per my requirements it is fine to have + or # before the '@'. I know this sounds odd. – Yeshwanth Kota Jul 06 '12 at 11:39