Javascript: How to Redirect a Page After Validation

JavaScript: How to redirect a page after validation

Try putting redirect in a timeout. Works like a charm

setTimeout(function() {window.location = "http://www.google.com/" });

By the way, instead of

onsubmit="return checkform(this);"

use

onsubmit="return(checkform())"

because IE doesn't like when you ommit ( and ).

How to redirect after form validation?

Put the redirect in the validation function before form.submit

How to redirect to another page after validation? And also want to pass the values to the next page

You need to use history to programmatically navigate inside your app:

import { useHistory } from 'react-router-dom';

let history = useHistory();

...
if(Username=='admin' || Username=='admin1' || Username=='admin2'){
history.push('/AdminPage');
} else {
history.push('/'); // redirect to home page
}

Form redirection after validating

Try to add a boolean variable that becomes false if the data is not valid like this:

$(document).ready(function() {
//This is to fill the inputs from the login form in the current registration form. Not important, this works fine.
$("#nif").val(localStorage.getItem("nif"));
$("#nombre").val(localStorage.getItem("nombre"));
$("#apellidos").val(localStorage.getItem("apellidos"));

//this is the submit button, enviar is "send".

$('#enviar').click(function(){
var valid = true;
// your validations
if($("#nif").val() == ""){
valid = false;
}
// and so on all your validations
if(valid){ // only if no errors
alert("User has been registered succesfully, will be redirected to the gallery.");
window.open("galeria.html");
}

});
});

If you want to redirect instead of opening a new window you can use:
location.replace("galeria.html");



Related Topics



Leave a reply



Submit