tags:

views:

98

answers:

2

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

[email protected]

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*)+,$
+2  A: 

It seems to me you'd better split the string up in pieces with a regex like \s*,\s* first, and then validate each domain seperately. Trying to push it all into a single regex will make your life hard if you try to change the logic later.

The domain validating regex that you have should work. I don't know if you are intentionally disallowing some valid domains. A more generic domain matching regex would be:

^[\w-]+(\.[\w-]+)+$

But even this one disallows localhost, which may or may not be allowed in your case. It is too loose for the specs in other respects. Optionally replace each [\w-]+ with [a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]? for more strictness and more obfuscation -- although it still does not invalidate numeric-only records, which are invalid according to the spec. How exact do you need it to be?

Disregarding the stuff about splitting the string first, based on my example regex above, you could use the following monster to match a list of domains:

^\s*([\w-]+(\.[\w-]+)+\s*,\s*)*[\w-]+(\.[\w-]+)+\s*$
molf
+1  A: 

"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

Jumipo