1

I want the regular expression to validate multiple email address for a long text field separated by comma but validation should be put so that field should accept the data in the email ID format (ex: [email protected],@[email protected],[email protected])only.

I have tried the regular expression:-

^([A-Za-z]+[A-Za-z0-9.-]+@(([aig.com])|([abc.in])|([abc.uk]))+\.[A-Za-z]{2,4}(\s*(;|,)\s*|\s*$))+$)

This expression is also accepting the email id if user enters (ex: [email protected],[email protected])

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
Bhumika
  • 17
  • 3
  • 1
    Regular expressions [don't make good parsers for email addresses](/q/201323/4850040). Are you really unable to use a more appropriate parser? – Toby Speight Apr 04 '23 at 15:13
  • 1
    Does this answer your question? [Regular expression to validate comma separated email addresses?](https://stackoverflow.com/questions/6870223/regular-expression-to-validate-comma-separated-email-addresses) – Toby Speight Apr 04 '23 at 15:14
  • What does "This expression is also accepting the email id if user enters (ex: [email protected],[email protected])" mean? I'm not entirely sure how that's different from what you were asking for before. You may want to specify what kind of input you'll have, and what kind of output you want. – Brock Brown Apr 04 '23 at 15:16

1 Answers1

0

You can use this to validate specified list:

^(?:(?:[A-Za-z][A-Za-z0-9.-]*@(?:aig\.com|abc\.in|abc\.uk))(?:,(?!$)|$))*$

Demo here. And if negative lookaheads are not acceptable, than this:

^(?:(?:[A-Za-z][A-Za-z0-9.-]*@(?:aig\.com|abc\.in|abc\.uk)),)*(?:[A-Za-z][A-Za-z0-9.-]*@(?:aig\.com|abc\.in|abc\.uk))$

Worth noting: (?:aig\.com|abc\.in|abc\.uk) matches any of aig.com, abc.in, abc.uk.

In your attempt [] where not needed as [aig.com] matches a, acc.cc.s or any permutation of symbols inside square brackets.

Also (?:,(?!$)|$) matches either , (if it's not last symbol of the line) or end of the string.

markalex
  • 8,623
  • 2
  • 7
  • 32