I am using web api in my MVC project and I have ran into a problem where if the user is on the create page.. fills out the form... and hits submit.. during the processing time, the user is able to continuously click the submit button for multiple creations.
My goal
Only allow the user to submit the form once. Basically, after the user clicks, or hits enter on the keyboard or however the user submits the form.. it should only allow 1 time.
I have researched this.
How do I implement that though? Here is what I have so far:
<input id="Edit-Btn-Submit" type="submit" value="Save" class="btn btn-primary" />
$("form").data("validator").settings.submitHandler =
function(form) {
$.ajax({
method: "PUT",
url: infoGetUrl + itemId,
data: $("form").serialize(),
beforeSend: function() {
disableSendButton();
},
complete: function() {
enableSendButton();
},
success: function() {
toastr.options = {
onHidden: function() {
window.location.href = newUrl;
},
timeOut: 3000
}
toastr.success("Equipment successfully edited.");
},
error: function(jqXHR, textStatus, errorThrown) {
var status = capitalizeFirstLetter(textStatus);
var error = $.parseJSON(jqXHR.responseText);
//console.log(error);
var modelState = error.modelState;
//console.log(error.modelState);
$.each(modelState,
function(key, value) {
var id = "";
if (key === "$id") {
id = "#" +
key.replace('$', '').substr(0, 1).toUpperCase() +
key.substr(2);
} else {
id = "#" +
key.replace('$', '').substr(0, 1).toUpperCase() +
key.substr(1);
var status = capitalizeFirstLetter(textStatus);
toastr.error(status + " - " + modelState[key]);
}
var input = $(id);
console.log(id); // result is #id
if (input) { // if element exists
input.addClass('input-validation-error');
}
});
}
});
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function disableSendButton() {
$('#Edit-Btn-Submit').prop('disabled', true);
}
function enableSendButton() {
$('#Edit-Btn-Submit').prop('disabled', false);
}
});
Any help is appreciated.