...

View Full Version : Need help with parsing a URL list to get the host names



venture89
07-17-2011, 09:14 PM
Hello All,

I have a form where I ask my users for a comma seperated web site list.
I want to make sure this list only contains the host names of URLs without any white spaces

Original list: site1.com, http://site2.com, http://www.site3.com,sub.site4.com
Edited list: site1.com,site2.com,site3.com,sub.site4.com

Currently I have the following to get the comma separated list


var list = document.getElementById( "list" ).value ;
Can some one please help me with above task
I highly appreciate your help or direction.

siberia-man
07-17-2011, 10:33 PM
First of all you need transform a string presentation of a list to a real list (I mean an array):


var list = document.getElementById('list').value.split(/\s*,\s*/);


After that you need walk through each item and validate that it contains a hostname with no any part of URI:


/*
This regular expression considers
these strings are valid
http://hostname
http://hostname/
hostname
hostname/

and others are invalid
ex.:
http://hostname/part

etc...
*/
var re = /^(?:\w+:\/\/)?([^\/]+)\/?$/;


Now you need assemble all parts above with the following code and put the actual modifications:


for (var i = 0; i < list.length; i++) {
var m = list[i].match(re);
if ( ! m ) {
// FAIL!!! Inform a user about invalid input
}
// OK!!! Do something with a valid entry
// m[1] contains a hostname only
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum