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

1

2 Answers 2

1

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

0
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?

1
  • 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. Commented Jul 6, 2012 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.