Display a confirmation message upon successful validation of a form using Bootstrap 4

After trying to implement a success modal that shows up upon successful form validation using Bootstrap 4 and its JavaScript code, I made a slight modification. However, I am uncertain if this is the correct approach to handle it.

I believe there might be an issue with my event.showModal implementation.

<div class="row content">
    <div class="container">
        <div class="col">
            <form id="form" class="needs-validation" novalidate>
              <div class="form-group">
                <label for="exampleInputEmail1">Your Name <em class="required">*</em></label>
                <input type="Name" class="form-control" id="exampleInputName" aria-describedby="nameHelp" required>
                <div class="invalid-feedback">
                    Please add your name
                </div>
              </div>
              <div class="form-group">
                <label for="exampleInputPassword1">Your Email <em class="required">*</em></label>
                <input type="Email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"  required>
                <div class="invalid-feedback">
                    Please add a valid email
                </div>
              </div>
              <div class="form-group">
                <label for="exampleInputPassword1">Label <em class="required">*</em></label>
                <textarea class="form-control" id="exampleFormControlTextarea1" rows="3" required></textarea>
                <div class="invalid-feedback">
                    Please add some details
                </div>
              </div>
              <div class="form-group">
                <label for="exampleInputPassword1">Anything else you want us to know? <em class="required">*</em></label>
                <textarea class="form-control" id="exampleFormControlTextarea2" rows="6" required></textarea>
                <div class="invalid-feedback">
                    Just do it…
                </div>
              </div>

              <button type="submit" class="btn btn-primary btn-givi hvr-grow-shadow hvr-back-pulse">Submit</button>
            </form>
        </div>
        <div id="success" class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
          <div class="modal-dialog modal-sm">
            <div class="modal-content">
              <h1>Success</h1>
            </div>
          </div>
        </div>


(function() {
 'use strict';
  window.addEventListener('load', function() {
   // Fetch all the forms we want to apply custom Bootstrap validation styles to
   var forms = document.getElementsByClassName('needs-validation');
   // Loop over them and prevent submission
   var validation = Array.prototype.filter.call(forms, function(form) {
      form.addEventListener('submit', function(event) {
       if (form.checkValidity() === false) {
         event.preventDefault();
         event.stopPropagation();
       }
        else if (form.checkValidity() = true) {
           event.showModal('#Sucess');
        }
       form.classList.add('was-validated');
      }, false);
    });
  }, false);
})();

Answer №1

Utilize $('#success').modal("show")...

  if (form.checkValidity() === false) {
     event.preventDefault();
     event.stopPropagation();
  }
  else if (form.checkValidity() == true) {
       $('#success').modal("display");
  }

Live example:

It's important to mention that the condition for true has been modified...

(form.checkValidity() = true) should be (form.checkValidity() == true)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The callback function in JavaScript is not updating AngularJS unless it is written in shorthand form

Within an angular controller designed for user login functionality, the code snippets below are extracted from an angular-meteor tutorial: this.login = function() { Meteor.loginWithPassword(this.credentials.email, this.credentials.password, (e ...

Having difficulty arranging the material ui components in a left-to-right alignment

Hello, I need assistance with my bilingual website that supports English and Arabic. I am looking to align the MUI text fields and dropdowns in a right-to-left direction. The labels of the text fields and dropdowns are not styled properly. Currently, I am ...

Adding elements to a JavaScript modal using a loop

When working with JavaScript, I am dynamically adding content to a modal. This involves populating different data into various table rows and cells within the modal. <table class="table table-striped"> <thead> <tr> ...

Can you please explain the significance of the code "!!~this.roles.indexOf('*')" within the MEAN.io framework?

One particular line of code can be found in the startup file for the MEAN framework. if (!!~this.roles.indexOf('*')) { This specific line is located within the shouldRender function of the menus.client.service.js file, which resides in the publ ...

Utilize Firebase Hosting to Host Your Vue Application

Having some trouble with hosting code on Firebase. Instead of displaying the value, {{Item.name}} is appearing :( Same code works fine on Codepen. Wondering if Firebase accepts vue.min.js? When deployed, the site is showing {{var}} instead of the table va ...

What are some strategies for sorting information from a list that is constantly changing?

I have been working on a web application built in asp.net that receives data from a web service in JSON format. The current task is to dynamically develop controls for this application. I achieved this by creating a list of labels with stored values using ...

Received an error while using an Express router: "Unable to access property 'caseSensitive' of undefined."

javaScriptCode app.js const express = require('express') const app = express() const {route} = require('./routes/route') app.use(express.static('./public')); app.use(express.json()); app.use(express.urlencoded()); app.use(rout ...

sketch a portion of a picture on a canvas

My challenge involves a canvas with dimensions of 400 by 400, and an image sized at 3392 by 232. I am looking to crop the first 400 pixels of this image and display them within the canvas. How can I achieve this task? $(document).ready(function () { v ...

Managing errors and error codes in React/Redux applications

I am currently exploring the most effective approach for managing errors, particularly when deciding between error codes and an errors object in response from my API. As I dive into building a multi-part form using react, each time a user progresses to th ...

In search of assistance in delivering HTML form data to a public HTTP service

Seeking assistance with forwarding html form data to a public http service. Any guidance or tutorial recommendations would be greatly appreciated! Below is the structure of my form: <span class="text"> <input class="text" type="text" name="First ...

OBJLoader encountered an unexpected line: "<!DOCTYPE html>" while using vue cli3

Utilizing the vue-3d-model component for a 3D object viewer in cli3 has presented a challenge that needs to be addressed. <script> // import { ModelThree } from 'vue-3d-model' import { ModelObj } from 'vue-3d-model' ...

The EJS view fails to render when called using the fetch API

Within my client-side JavaScript, I have implemented the following function which is executed upon an onclick event: function submitForm(event) { const data = { name, image_url }; console.log(data); fetch('/', { method: &apo ...

Utilizing React Router: Combining MemoryRouter and Router for Dynamic Routing (leveraging memory for certain links while updating the URL for others)

While utilizing react-router-dom, I came across the helpful <MemoryRouter>. However, I am in need of having several routes that can read and write to/from the browser's URL. What is the best approach for implementing this functionality? Thank y ...

Issue with post-processing filters in Three.JS r71: Transparent renderer is not functioning as expected

I am attempting to implement a BloomPass on my current scene and want the background of the containing page to show through. However, when I apply the BloomPass, the background turns black. You can see my example here:http://plnkr.co/edit/0mp0jaGVF6it52HY ...

When a custom icon is clicked in the vue-select dropdown, the custom method is not activated

In my current project, I am creating a vue-component based on vue-select. Within this component, I have added a custom info Icon. The issue I am facing is that when the user clicks on the Icon, instead of triggering my custom method getInfo, it opens the s ...

Customize the font color in Material UI to make it uniquely yours

How can I customize the default Text Color in my Material UI Theme? Using primary, secondary, and error settings are effective const styles = { a: 'red', b: 'green', ... }; createMuiTheme({ palette: { primary: { ...

Instructions for swapping out the default drop-down icon in my select box with the <i class="icon-xyz-chevron down-rotation"></i> symbol

Is there a way to replace the default drop down arrow with a new one without creating a new class and adding it to the label? <i class="icon-xyz-chevron down-rotation"></i> Any suggestions on achieving this? <label class="col-md-8 col-sm- ...

"Troubleshooting the issue of nested jQuery Ajax requests failing to execute properly

UPDATE: I'm getting an error saying that my li tag is considered an illegal token. I'm unsure how to resolve this issue as I believed I was appending it within a ul tag I'm tasked with fetching a JSON array of public Github repositories and ...

The getMonthlyBalances function is providing inaccurate results when calculating stock balances

One of my functions is called getMonthlyBalances, which takes in two arrays - stocks and trades. It calculates the monthly balance of a user's stock holdings based on their trade history. The stocks array includes objects with stock ID, prices, and da ...

working with received data from a JavaScript object

Looking for code assistance, I have a data object serving as my data source. var data = [ { "label":"May 7", "value":25736.6, "proID":"ISB" }, // more data objects... ]; I'm manipulating this data to generate another ...