0

I'm supposed to create a functions to test a URL for validity then functions to look for and return parts of the URL string based on location of certain characters (position would be unknown). FYI, I'm very new to programming but have been searching and trying many answers. My latest attempt uses below format (found in an answer) but still can not get anything but an empty string to display when I call the function.

When I run this in Chrome, and enter "http://www.niagaracollege.ca" or "http://lego.ca" even though I am entering a valid URL, I get a return of false.

function validURL(userInput)
{
    input = new String(userInput);

    if (input.indexOf("://") != -1 && input.lastIndexOf(".") != -1)
        return true;

    else
        return false;
  }

 function findProtocol(userInput)
{
    input = new String(userInput);
    var result = input.substring(0, input.indexOf("://"));
    return result;
}

function findServer(userInput)
{   
    input = new String(userInput);

    var result = input.substring(input.indexOf("://") + 1 ,input.lastIndexOf("."));

    return result;
}

function findDomain(userInput)
{
    input = new String(userInput);

    var result = input.substring(input.lastIndexOf(".") + 1);

    return result;
}

function btnReadURL_onclick()
{
    var userInput = document.getElementById("txtURL").value;

    var outputBox = document.getElementById("txtOutput");

    var URL = validURL(userInput);

    if (URL = true)
    {
       var Part1 = findProtocol(userInput);
       var Part2 = findServer(userInput);
       var Part3 = findDomain(userInput);

       outputBox.value = "Protocol: " + Part1 + "\nServer: " + Part2 + 
    "\nDomain: " + Part3;
    }   

    else (URL == true)
       outputBox.value = "Invalid URL"; 
 }
Tyzer
  • 3
  • 1
  • 4

2 Answers2

1

Use a debugger to find out what you are getting in the userInput. The code is fine. It should work. See sample code below.

test = function() {
    var test = "http://Test 2"
    alert(test.substring(0, test.indexOf("://")))
}
user883986
  • 21
  • 1
0

You need to pass the value to the findProtocol method rather than DOM element

Replace

var userInput = document.getElementById("txtURL");

by

var userInput = document.getElementById("txtURL").value;

and replace

if (URL = true)

with

if( URL == true )
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • that solved the empty string problem. Does anyone know why I'm not getting any false returns on the validURL function? – Tyzer Mar 21 '16 at 11:16
  • I made second change. Now all my URL tests come back invalid even when they should be valid. What else is wrong? – Tyzer Mar 21 '16 at 11:25
  • http://www.lego.ca, http://ww.niagaracollege.ca. It shouldn't matter if the URL is an actual site, as the validity fuction is just checking if "://" AND "." both exist for it to be true. In both examples, I get the result of false. – Tyzer Mar 21 '16 at 11:43
  • @Tyzer because neither of these two examples has `://` in it – gurvinder372 Mar 21 '16 at 11:45
  • Sorry - I guess they needed to be in quotes - it automatically made them links but I didn't realize that. I entered "http://lego.ca" and then "http://niagaracollege.ca" – Tyzer Mar 21 '16 at 11:46
  • why will the comment area here not show the "http://" before the web address like I'm typing? – Tyzer Mar 21 '16 at 11:48
  • @Tyzer can you update your question to share what input did you tried with **updated code**? – gurvinder372 Mar 21 '16 at 11:49