Home >Web Front-end >JS Tutorial >How to Display a Confirmation Dialog for Form Submissions in JavaScript?

How to Display a Confirmation Dialog for Form Submissions in JavaScript?

DDD
DDDOriginal
2024-11-01 00:52:02904browse

How to Display a Confirmation Dialog for Form Submissions in JavaScript?

Confirm or Cancel Submission for Forms 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:

  • Inline Confirmation:
<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.

  • External Function with Validation:
<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn