I\'m trying to validate the content of a using JavaScript, So I created a
validate()
function, which returns
function validate() {
//Get the user input
var hostnames = document.getElementById('yourtextarea').value;
//Regex to validate hostname
var re = new RegExp(/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/);
//Trim whitespace
hostnames = hostnames.trim();
//Explode into an array
hostnames = hostnames.split(",");
//Loop through array & test each hostname with regex
var is_valid = true;
for (var i=0; i < hostnames.length; i++){
var hostname = hostnames[i].trim();
if (re.test(hostname)) {
is_valid = true; //if valid, continue loop
} else {
is_valid = false; //if invalid, break loop and return false
break;
}
} //end for loop
return is_valid;
} //end function validate()
Matches every example you indicated except "dom-ain.it, domain.com, domain.eu.org.something" because "something" is not valid.
JSFiddle: http://jsfiddle.net/nesutqjf/2/