I\'m trying to validate the content of a using JavaScript, So I created a
validate()
function, which returns
While @dandavis's answer/comment is impressive, lets break it down in to steps.
trim()
leading and ending spaces./\s+/g
. meaning find every white space occurring one or more times.,
or ,
. Split returns array.var domains = document.querySelector("textarea").value;
domains = domains.trim().replace(/\s+/g, " ").split(/\s?,\s/);
var domainsTested = domains.filter(function(element){
if (element.match(/^[a-zA-Z0-9][a-zA-Z0-9-_]{0,61}[a-zA-Z0-9]{0,1}\.([a-zA-Z]{1,6}|[a-zA-Z0-9-]{1,30}\.[a-zA-Z]{2,3})$/))
{
return element;
}
})
document.write(domainsTested.join(" | ")); //this is just here to show the results.
document.write("
Domainstring is ok: " + (domainsTested.length == domains.length)); //If it's valid then this should be equal.