-1

I have a basic email validation regex ^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})$ which checks for the email address is valid or not. I just wanted to check if i can make that valid for blank field as well.If the field is blank it should still work and if the filed has some value it should check according to the regex

Sagar
  • 538
  • 3
  • 9
Rahul k
  • 9
  • 2
  • 1
    That's not a correct expression; among other things, it doesn't work for e-mail addresses at IPs. Use a library function to do this instead of inventing your own. – chrylis -cautiouslyoptimistic- Oct 01 '19 at 17:50
  • 1
    possible of duplication of https://stackoverflow.com/questions/5063977/regex-empty-string-or-email . Please correct the heading, is Regex and not Rejex. – stacktome Oct 01 '19 at 17:53
  • 1
    Don't write this yourself, unless you want to accept `%%+%%__-----@..---...-.fart` as valid input. – CAustin Oct 01 '19 at 17:56
  • Please, have a look at these sites: [TLD list](https://www.iana.org/domains/root/db); [valid/invalid addresses](https://en.wikipedia.org/wiki/Email_address#Examples); [regex for RFC822 email address](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Toto Oct 01 '19 at 18:26

1 Answers1

2
  • You just need to add a ? at the end of your regex like this ^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})?$
  • Quantifier — Matches between zero and one times, as many times as possible, giving back as needed
amittn
  • 2,249
  • 1
  • 12
  • 19