]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Fix forms validation snippets.
authorXhmikosR <xhmikosr@gmail.com>
Sat, 2 May 2020 15:39:41 +0000 (18:39 +0300)
committerXhmikosR <xhmikosr@gmail.com>
Wed, 3 Jun 2020 12:57:20 +0000 (15:57 +0300)
site/assets/js/validate-forms.js [new file with mode: 0644]
site/content/docs/5.0/forms/validation.md

diff --git a/site/assets/js/validate-forms.js b/site/assets/js/validate-forms.js
new file mode 100644 (file)
index 0000000..b79f63f
--- /dev/null
@@ -0,0 +1,24 @@
+// Example starter JavaScript for disabling form submissions if there are invalid fields
+(function () {
+  'use strict'
+
+  // Fetch all the forms we want to apply custom Bootstrap validation styles to
+  var forms = document.querySelectorAll('.needs-validation')
+
+  if (!forms) {
+    return
+  }
+
+  // Loop over them and prevent submission
+  Array.prototype.slice.call(forms)
+    .forEach(function (form) {
+      form.addEventListener('submit', function (event) {
+        if (!form.checkValidity()) {
+          event.preventDefault()
+          event.stopPropagation()
+        }
+
+        form.classList.add('was-validated')
+      }, false)
+    })
+})()
index 2b8e1b7aa93882479f9fe8ca1f158ed9b88a4e94..c5051636249b32ef4d4385f79e052720c8313730 100644 (file)
@@ -96,8 +96,9 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
     <button class="btn btn-primary" type="submit">Submit form</button>
   </div>
 </form>
+{{< /example >}}
 
-<script>
+{{< example lang="js" show_preview="false" >}}
 // Example starter JavaScript for disabling form submissions if there are invalid fields
 (function () {
   'use strict';
@@ -118,7 +119,6 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
       }, false);
     });
 })();
-</script>
 {{< /example >}}
 
 ## Browser defaults