Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Need help with parsing a URL list to get the host names

    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

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

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    First of all you need transform a string presentation of a list to a real list (I mean an array):
    Code:
    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:
    Code:
    /*
    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:
    Code:
    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
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •