Try
$(document).ready(function() {
var attempts = 0; // number of Login attempts
var initialShakes = 3; // number of shakes on first failed attempt
var maxShakes = 10; // maximum number of shakes
// Login form submit event
$("#loginForm").submit(function() {
var form = $(this); // use this since it points to the submitted form
$.ajax({
type : form.attr("method"), // there is no type attribute, it is method in the form
url : form.attr("action"),
data : form.serialize(),
success : function(data) {
}
}).fail(function() { //change the error handler to use ajax callback because of the async nature of Ajax
alert("false");
// Shake the form because Login is not valid
shakeLoginForm();
attempts++;
}).done(function() {
alert("true");
// Your code for valid login goes here.......
});
return false;
});
function shakeLoginForm() {
// Determine how many times the form will shake.
// Initially, it will start from the value of initialShakes,
// then increase by 1 on every failed attempt.
// The following assures that the number
// of shakes will not exceed the specified maximum.
var shakeTimes = Math.min(initialShakes + attempts, maxShakes);
// The single line code to shake the form.
$("#loginForm").effect("shake", {
times : (shakeTimes)
});
}
});