Home >Web Front-end >JS Tutorial >How to Display a Confirmation Dialog for Form Submissions in JavaScript?
Problem:
How to display a confirmation dialog box when submitting a form, with options to proceed or cancel?
Solution:
JavaScript's built-in confirm function can be used directly within the form's onsubmit event handler to achieve this functionality.
Implementation Options:
<code class="html"><form onsubmit="return confirm('Do you want to submit the form?');"></code>
This inline confirmation will directly prompt the user for confirmation.
<code class="html"><form onsubmit="return validateForm();"></code>
<code class="javascript">function validateForm() { // Perform form validation here. if (!isValid) { alert('Please correct errors in the form!'); return false; } else { return confirm('Do you want to submit the form?'); } }</code>
This method allows for additional validation checks before displaying the confirmation dialog box.
The above is the detailed content of How to Display a Confirmation Dialog for Form Submissions in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!