I\'m trying to validate the content of a using JavaScript, So I created a
validate()
function, which returns
I would not use regexps for this, because you have a lot of different rules you want to check. Regexps are good when you only have a couple of rules that are very simple to express but a pain to write out as "parsing code".
I'd simply do hostnames.split(',').forEach(validateHostname);
, as most of the comments suggest, and inside validateHostname
reject any hostname that has spaces in the middle, two adjacent dots, no dots, ends in a dot, has non-ASCII characters, has digits in the last dot-separated token, and so on and so forth.
A function like this will be much easier to add new rules to than a regexp would be.