0

I'm trying to validate a field with comma separated list of strings(particular form) or empty. I can't use split.

I've managed to have a regular exp for a single string.

I just want to implement comma separation(with spaces before and after , may be) or having empty string. If it is not empty it must have the particular string format. Here if more than one string is entered then it should use comma.

The string format is

   ^([a-z_\.\-])+\@(([a-z\-])+\.)+([a-z]{2,4})+$

Pls help me to add comma separation and a whole empty string. Thanks

miken32
  • 42,008
  • 16
  • 111
  • 154
itsraja
  • 1,640
  • 4
  • 32
  • 48
  • 1
    Which language are you using, or which dialect of regular expression? Your people with an email address in the `.museum` TLD aren't going to be happy that you reject their email, and that is only going to get worse as ICANN gets more inventive. – Jonathan Leffler Jun 21 '12 at 04:00
  • 1
    Not 100% related, but is this a regex for email addresses? That problem is [notoriously tricky](http://stackoverflow.com/questions/201323/how-to-use-a-regular-expression-to-validate-an-email-addresses), for example, there are [email address](http://www.ncm.museum/contact_us) with `.museum` TLD and these would fail your validation. – huon Jun 21 '12 at 04:00
  • @dbaupp: hey, were you reading over my shoulder :D – Jonathan Leffler Jun 21 '12 at 04:00
  • ya I can change like 2,7 . but what I'm concerned about now is if more that 1 then comma . if no string at all, ok. that's it. This is with java environment but I'm specifying it in an xml entity. – itsraja Jun 21 '12 at 04:03
  • (@JonathanLeffler, haha :D ) What have you tried in terms of adding a comma? e.g. Can you write a regex that matches a list of things separated by commas, not worrying about the empty string yet? – huon Jun 21 '12 at 04:11
  • like (^([a-z_\.\-])+\@(([a-z\-])+\.)+([a-z]{2,4})+$)*,? – itsraja Jun 21 '12 at 04:14

1 Answers1

2

Try this expression

/^$|^([a-z_\.\-])+\@(([a-z\-])+\.)+([a-z]{2,4})+(\s*,\s*([a-z_\.\-])+\@(([a-z\-])+\.)+([a-z]{2,4})+)*$/

The pattern is ^$|^string(,string)*$

Leonid
  • 3,121
  • 24
  • 31