December 17, 2022
Do you have a form that isn't supposed to be submitted multiple times by the same user? Use this snippet to prevent that from happening.
The following code expects your form to have an ID of "form", but can be changed to anything. When a user submits the form, a variable called "formSubmitted" with a value of "true" will be saved in the users browser's local storage. If this variable is true, the next time a user tries to submit the form, an alert pop up with a message will be triggered instead of submitting the form.
The alert can be replaced with anything you want, such as an error message.
$(document).ready(function () {
$("#form").submit(function (event) {
if (localStorage.getItem("formSubmitted") === "true") {
event.preventDefault();
alert(
"The form has already been submitted. Thank you for your interest."
);
} else {
localStorage.setItem("formSubmitted", "true");
}
});
});