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.

Sign up to request clarification or add additional context in comments.

Comments

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 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.