John123 John123 - 1 month ago 9
CSS Question

Javascript redirect upon condition fulfillment

I have an html page with a section for input and a submit button. The section for input takes in a string. The string is then passed into the javascript function and when the "submit" button is clicked, an if statement is used to check the length of that string. If the string has a length of 20 characters, it theoretically should redirect the user to a different website. This is the code I have to accomplish this:



function validate() {
var value = document.getElementById('myfunction').value;
if (value.length == 20) {
window.location.href ="http://www.example.com";
}
else{
return false;

}
}

<input style = "margin-right: 8px;" type = "text" name = "con" id = "myfunction" required=''/>
<input type = "submit" value = " Submit " class='next btn btn-primary' id = "buttonze" onclick = "validate();"/><br />





After running the code with a string that is 20 characters, nothing happens. For some reason it doesn't redirect me to example.com. I'm not sure what I'm doing wrong and was hoping someone could point it out to me. I know its executing the if statement because I placed an alert statement to test it and it was successfully popping up when I entered the statement.

Answer

If you using safari, you need to add return false to onclick:

onclick = "validate();return false;"
Comments