Bala
Bala

Reputation: 11

How to go to another HTML page, if form is validated and stay in the same page if it is not validated?

So I have a HTML file where I collect some input, and I validate it using JavaScript. If I give the wrong type of input I get the alert window which I have given. When I press 'ok' in the alert window it goes to the target page instead of staying in the same page. How do I fix this error?

function validate()
{
 var x = document.getElementById("zc").value;
 if (x.toString().length != 6) {
    alert("Enter a valid  zip-code which contains 6 digits");
    x.focus();
    return False;
}
 else {
    alert("Form Success");
    return True;
}
}

My HTML file is given below, I have skipped the parts where I collected input. If it is false shouldn't be in the same page instead of going to welcom.html.

<form action="welcome.html" onsubmit="return validate()">
<button type="submit">Submit</button>
</form>

Upvotes: 0

Views: 816

Answers (1)

Jay Morelli
Jay Morelli

Reputation: 107

you could use the window.location.href method. I will leave a link from w3schools where it explains how to redirect to another page.

link: https://www.w3schools.com/howto/howto_js_redirect_webpage.asp

Upvotes: 1

Related Questions