How to prevent duplicate submission in a form using jQuery?
Last Updated :
28 Mar, 2024
Preventing duplicate submission in a form using jQuery involves implementing measures to ensure that the form is only submitted once, regardless of how many times the user interacts with the submit button. To prevent duplicate form submissions in web applications using jQuery we can use various methods like One-time event binding and disabling submit button.
Use the below approaches to prevent duplicate submissions in a form using jQuery:
In this approach, jQuery's .one() method ensures that the form submission event is bound only once, preventing duplicate submissions. Upon submission, it disables the form elements and displays a success message. Further form submissions are blocked after the initial submission, ensuring data integrity.
Example: Implementation of preventing duplicate submission in a form with jQuery using jQuery One-Time Event Binding.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Prevent Duplicate Form Submission</title>
<style>
.navbar {
background-color: #333;
padding: 10px 0;
width: 100%;
top: 0;
z-index: 1000;
}
.logo-container {
display: flex;
justify-content: center;
align-items: center;
}
.logo {
width: 80px;
height: auto;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
}
#content {
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
margin-top: 20px;
text-align: center;
}
form {
width: 300px;
margin-top: 20px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
input[type="text"],
input[type="password"],
button {
width: 100%;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
button {
background-color: #007bff;
color: #fff;
cursor: pointer;
}
button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
button:hover:enabled {
background-color: #0056b3;
}
#successMessage {
display: none;
color: green;
margin-top: 10px;
}
#submittedData {
display: none;
margin-top: 20px;
padding: 10px;
background-color: #f5f5f5;
border: 1px solid #ccc;
border-radius: 5px;
}
</style>
</head>
<body>
<nav class="navbar">
<div class="logo-container">
<img class="logo" src=
"https://media.geeksforgeeks.org/gfg-gg-logo.svg" alt="Logo"/>
</div>
</nav>
<div id="content">
<h1>Using jQuery One-Time Event Binding to prevent
duplicate form submissions
</h1>
<form id="myForm">
<input type="text" name="username"
placeholder="Username" required>
<input type="password" name="password"
placeholder="Password" required>
<button type="submit">Submit</button>
</form>
<div id="successMessage">
Form submitted successfully!
</div>
<div id="submittedData"></div>
</div>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
$(document).ready(function () {
$('#myForm').one('submit', function (event) {
event.preventDefault();
// Display success message
$('#successMessage').show();
// Show submitted data
let formData = $(this).serialize();
$('#submittedData').html(
'<strong>Submitted Data:</strong><br>'
+ formData).show();
// Disabling form fields and button
$(this).find('input, button')
.prop('disabled', true);
});
});
</script>
</body>
</html>
Output:
OutputThis approach involves using jQuery, to disable the submit button immediately upon form submission. By disabling the button, users are unable to click it multiple times, effectively preventing duplicate form submissions, enhancing user experience and preventing unintended actions.
Example: Implementation of preventing duplicate submission in a form by disabling the submit button.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Prevent Duplicate Form Submission</title>
<style>
.navbar {
background-color: #333;
padding: 10px 0;
}
.logo-container {
display: flex;
justify-content: center;
align-items: center;
}
.logo {
width: 80px;
height: auto;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js">
</script>
<script>
$(document).ready(function () {
$('#myForm').submit(function (event) {
// Prevent default form submission
event.preventDefault();
// Check if the form has already been submitted
if ($(this).data('submitted')) {
return;
}
// Disable the submit button
$('#submitBtn').prop('disabled', true);
let formData = $(this).serialize();
$('#submittedData').html(formData);
$('#statusMessage').text('Submitting...');
$(this).data('submitted', true);
setTimeout(function () {
$('#statusMessage').text('');
}, 3000);
});
});
</script>
</head>
<body>
<nav class="navbar">
<div class="logo-container">
<img alt="Logo" class="logo" src=
"https://media.geeksforgeeks.org/gfg-gg-logo.svg" />
</div>
</nav><br />
<h2>Prevent duplicate submission
in a form using jQuery
</h2>
<form id="myForm">
<input type="text" name="name"
placeholder="Name" required><br>
<input type="email" name="email"
placeholder="Email" required><br>
<input type="submit" id="submitBtn"
value="Submit">
</form>
<p id="statusMessage"></p>
<div id="submittedData">
</div>
</body>
</html>
Output:

Similar Reads
How to submit a form using ajax in jQuery ? Submitting a form using AJAX in jQuery allows sending form data to a server asynchronously without reloading the page. This method improves user experience by sending the data in the background and receiving a response without interrupting the user's interaction with the webpage.Syntax:$.ajax({type:
2 min read
How to stop a form submit action using jQuery ? In this article, we will learn how to stop a form submit action using jQuery. By default, the HTML form submits automatically. Submitting automatically leads to reloading the whole page again and again. Therefore for performing any operation, we have to prevent its default submission. Given a form,
3 min read
How to submit a form on Enter button using jQuery ? Given an HTML form and the task is to submit the form after clicking the 'Enter' button using jQuery. To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. html <!DOCTYPE html>
2 min read
How to disable form submit on enter button using jQuery ? There are two methods to submit a form, Using the "enter" key: When the user press the "enter" key from the keyboard then the form submit. This method works only when one (or more) of the elements in the concerned form have focus. Using the "mouse click": The user clicks on the "submit" form button.
2 min read
How to clear form after submit in Javascript without using reset? Forms in JavaScript serve to gather specific user information, essential in various contexts like recruitment or inquiry submissions. While offline forms can be freely distributed, online forms must maintain a consistent structure for all users. After filling out a form, submission is crucial, but d
2 min read
How to disable or enable form submit button in jQuery? Enabling or disabling the form submit button based on a checkbox selection in jQuery. This ensures user compliance with terms and conditions before form submission. Syntax:$('#enabled').click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button').removeAttr('disabled'); } else
2 min read
How to create a Text Input using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be a Text Input using jQuery Mobile. Approach: First, we add the jQuery Mobile scripts needed for the project. <link rel=âstylesheetâ hre
1 min read
How to make an Email Input using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets,an and desktops. In this article, we will be creating an Email Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hr
1 min read
How to create a Disabled Input using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Disabled Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hre
1 min read
How to perform jQuery Callback after submitting the form ? The after-form-submit is the set of code that is executed after a submit button is fired. Usually, it is used to blank out the form, once the submit is given. For Example, When the login credentials are wrong, the input becomes blank in the form. Approach: First, the elements in a form must be ensur
2 min read