Reputation: 11663
$("#submit-button").click(function() {
if($.trim($("#email").val()) === "" ) {
$(".error-message-email").show();
}
if($.trim($("#password").val()) === ""){
$(".error-message-password").show();
}
return false;
});
$("#submit-button").click(function() {
$.ajax({
type:'POST',
url:" " ,
data:{"email": email, "password": password},
success:function(data) {
if(data["success"] === "True") {
// do something.
} else {
if(data["message"] === "Email already exists.") {
$(".error-message-email-exist").show();
}
}
},
dataType:"json"
});
return false;
});
});
I have problem. If error occured then how to stop ajax call to fire. Because here still my form failed to pass the validation, ajax call fired. I want to stop it.
Upvotes: 1
Views: 718
Reputation: 10294
Since you have binded click
twice and written the ajax call in the second one, it is firing regardless of the first one. It is totally independent.
Upvotes: 0
Reputation: 18419
don't separate your click event.
$("#submit-button").click(function() {
var error = false;
if($.trim($("#email").val()) === "" ) {
$(".error-message-email").show();
error = true;
}
if($.trim($("#password").val()) === ""){
$(".error-message-password").show();
error = true;
}
if(!error) {
$.ajax({
type:'POST',
url:" " ,
data:{"email": email, "password": password},
success:function(data) {
if(data["success"] === "True") {
// do something.
} else {
if(data["message"] === "Email already exists.") {
$(".error-message-email-exist").show();
}
}
},
dataType:"json"
});
}
return false;
});
Upvotes: 1
Reputation: 8337
Like this:
$("#submit-button").click(function() {
var error = false;
if($.trim($("#email").val()) === "" ) {
$(".error-message-email").show();
error = true;
}
if($.trim($("#password").val()) === ""){
$(".error-message-password").show();
error = true;
}
if (!error) {
$.ajax({
type:'POST',
url:" " ,
data:{"email": email, "password": password},
success:function(data) {
if(data["success"] === "True") {
// do something.
} else {
if(data["message"] === "Email already exists.") {
$(".error-message-email-exist").show();
}
}
},
dataType:"json"
});
}
return false;
});
Upvotes: 1