1

I want to validate an email id in angular 2 Now i am using

( /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/) 

But if we leave the field empty, it will remain invalid.

so can you suggest me an better option for validating email

3
  • How and where are you using it? Commented Jun 9, 2016 at 6:29
  • Refer this thread - stackoverflow.com/questions/34072092/… Commented Jun 9, 2016 at 6:35
  • What's the problem with the solution in this thread? Commented Jun 9, 2016 at 7:14

2 Answers 2

1

Add |(^$) at the end of your reg exp to accept empty string:

( /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$|(^$)/)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

(/^([0-9a-zA-Z]([_\\.]*[0-9a-zA-Z]+)*)@([0-9a-zA-Z]([-_\\.]*[0-9a-zA-Z]+)*)[\\.]([a-zA-Z]{2,9})$/)

1 Comment

Why are you limiting TLD to 9 characters? Have a look at iana.org/domains/root/db And why haven't you kept the special characters present in OP?

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.