This is for a textbox validation. I need to match on a list of domains
MATCHES
google.com, msn.com, texas.edu.gov.us
msn.com
NON-MATCHES
google.com, msn.com,
@msn.com, @google.com
without the trailing comma (that’s where I’m getting stuck)
This is what I have so far but the comma delimited part isn’t working right:
^([([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}]+\s*)+,$
“Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.”
– Jamie Zawinski
Here is a regex that will do what you want. It will handle a comma delimited list of domains, ensure no illegal characters are in the domain names, and enforce the length of domain name (I thought it was 63 not 61, but you have 61 so I left as that)
^\s*(([a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?(.[a-zA-Z0-9]{2,6})+)+\s*,\s*)*([a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?(.[a-zA-Z0-9]{2,6})+)$
*note [a-zA-Z0-9] is used instead of [\w] because underscores are included in \w but not allowed in domain names