1

I have the following html & js functions that will take values from an input form then concatenate the data to the end of a url string and the goal is to re-direct the url but my problem is that it is not re-directing. What am I doing wrong?

Heres the html

<form id="url">
            <input type="text" name="urlName">
            <button onclick="myFunction()">Try it</button>
        </form>

Heres the Javascript

function myFunction() {
    let myForm = document.getElementById("url");
    let formData = new FormData(myForm);
    EndOfUrl = sanitizeDomainInput(formData.get("urlName"));
    window.location = redirectLink(EndOfUrl);
    return false;
}

    function sanitizeDomainInput(input) {
        input = input || 'unknown.com'
        if (input.startsWith('http://')) {
            input = input.substr(7)
        }
        if (input.startsWith('https://')) {
            input = input.substr(8)
        }
        var regexp = new RegExp(/^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,3})$/)
        return regexp.test(input) ? input : 'unknown.com'
    }

    function redirectLink(domain) {
        return `https://dashboard.getorda.com/signup/?state=${domain}`;
    }

Please help

flexwithfrank
  • 35
  • 1
  • 7

3 Answers3

0

The return false in myFunction does nothing, you have to return false on the attribute itself if you want to prevent the form from submitting and ignoring your redirection.

<button onclick="myFunction();return false;">Try it</button>

or

<button onclick="return myFunction()">Try it</button>

or since there is no intention for the form to ever submit set the button type so that it doesn't submit the form.

<button type="button" onclick="myFunction()">Try it</button>
Musa
  • 96,336
  • 17
  • 118
  • 137
0

Please update the button.

<button type='button' onclick="myFunction()">Try it</button>

The rest seems to be okay.

-1

Please move the button to the outside of the form. When the button is inside of the form, it works as form submissions. myFunction cannot run if the button is inside of the form.

<form id="url">
     <input type="text" name="urlName">
</form>
<button onclick="myFunction()">Try it</button>
Gary Liu
  • 1
  • 1