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: xyz@aig.com,@xyz@abc.in,xyz@abc.uk)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: abc@aig.uk,abc@aig.in)

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: abc@aig.uk,abc@aig.in)" 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
  • Hi, This regex is working for me but one error is occurred. If i enter value (ex:- abc@aig.com, OR abc@aig.com,xyz@abc.uk,). This is accepting comma if no values passed after the comma. But the vaildation should be failed if no value is entered after comma. Can you please help me to solve this query – Bhumika Apr 28 '23 at 09:37
  • @Bhumika, corrected initial suggestion (by adding negative lookahead), and also added regex for case if lookaheads are not desirable. – markalex Apr 28 '23 at 10:06