bootstrap-validation.js

🌐 Overview

This lightweight script enables HTML5 form validation styling across all forms on the page. It prevents form submission when invalid and applies Bootstrap’s validation classes to trigger CSS feedback. Ideal for modern browsers, it leverages native validation APIs without external dependencies.

⚙️ Functionality

  • Listens for the DOMContentLoaded event.
  • Selects every <form> element in the document.
  • Attaches a submit event listener to each form.
  • Calls form.checkValidity() to perform native HTML5 validation.
  • Prevents submission (event.preventDefault()) if the form is invalid.
  • Adds the .was-validated class to style validation states .

🎯 Intended Use

  • Works with Bootstrap 4/5 CSS to display valid/invalid input states.
  • Applies to any form on the page—no need to mark forms with special classes.
  • Complements or replaces heavier validation libraries (e.g., jQuery Validate).

🚀 Integration

Include this script after loading Bootstrap’s JavaScript (or before your closing </body> tag):

<script src="path/to/bootstrap.min.js"></script>
<script src="new_2025_js/bootstrap-validation.js"></script>

Ensure your forms have the novalidate attribute to suppress the browser’s default UI:

<form novalidate>
  <!-- form controls… -->
</form>

In legacy setups, this file was referenced (but commented out) in the script aggregator configuration .

🧩 Dependencies

Dependency Description
None (vanilla JS) Uses built-in DOM and HTML5 form APIs.
Bootstrap CSS Styles .was-validated, .is-valid, and .is-invalid.

📝 Code Walkthrough

  1. DOMContentLoaded: Waits until the DOM is fully parsed.
  2. Query Forms: document.querySelectorAll('form') gathers all forms.
  3. Loop & Bind: Converts the NodeList into an array and iterates.
  4. Submit Handler:
    • Calls form.checkValidity().
    • If false, calls event.preventDefault() and event.stopPropagation().
    • Always adds .was-validated to trigger Bootstrap styling.
  5. Passive Flag: The false parameter ensures the handler runs in the bubble phase .

🔧 Usage Example

<form class="needs-validation" novalidate>
  <div class="mb-3">
    <label for="email" class="form-label">Email address</label>
    <input type="email"
           class="form-control"
           id="email"
           placeholder="[email protected]"
           required>
    <div class="invalid-feedback">
      Please provide a valid email.
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit</button>
</form>

Include the script:

<script src="bootstrap.min.js"></script>
<script src="bootstrap-validation.js"></script>

📊 Comparison to jQuery Validation

  • bootstrap-validation.js
    • ✅ Uses native HTML5 APIs
    • ✅ Zero dependencies
    • ⚠️ Less customizable error messages
  • jquery.validate.custom.js (legacy)
    • ✅ Rich rule definitions & messages
    • ❌ Requires jQuery & plugin overhead

💡 Tips

  • Always add novalidate to your forms to hide the browser’s default validation UI.
  • Pair with .needs-validation (Bootstrap convention) for explicit intent.
  • Customize feedback styles via Bootstrap’s utility classes.

🎉 Conclusion

bootstrap-validation.js brings simple, effortless client-side validation styling to your forms. By leveraging modern browser APIs and Bootstrap CSS, it offers a lightweight alternative to heavier form-validation libraries. Enjoy cleaner code and faster load times!