Tuesday, 20 August 2013

Jquery: return: false on submit not working - form still submits

Jquery: return: false on submit not working - form still submits

Is there a reason that the form is still submitting? The validation checks
work but if it everything is input correctly, the form submits and the
ajax request does not fire.
$("#formRegister").submit(function () {
var username = $("#registerUsername").val();
var password = $("#registerPassword").val();
var passwordConfirm = $("#registerPasswordConfirm").val();
var avatar = $("#registerAvatar").val();
if (username == 'Username') {
$("#registerError").html('You must enter a username');
} else if (password == 'Password') {
$("#registerError").html('You must enter a password');
} else if (password != passwordConfirm) {
$("#registerError").html('Your passwords did not match');
} else if (avatar == 'Avatar URL') {
$("#registerError").html('You must enter the URL for your combine
avatar');
} else {
$.ajax({
type: "POST",
url: "processUsers.php",
data: {
mode: mode,
username: username,
password: password,
avatar: avatar
},
dataType: "JSON",
success: function(data) {
alert('success!');
}
});
}
return false;
});

No comments:

Post a Comment