mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-22 05:03:16 +02:00
Fix forms validation snippets.
This commit is contained in:
24
site/assets/js/validate-forms.js
Normal file
24
site/assets/js/validate-forms.js
Normal file
@@ -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)
|
||||||
|
})
|
||||||
|
})()
|
@@ -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>
|
<button class="btn btn-primary" type="submit">Submit form</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
{{< /example >}}
|
||||||
|
|
||||||
<script>
|
{{< example lang="js" show_preview="false" >}}
|
||||||
// Example starter JavaScript for disabling form submissions if there are invalid fields
|
// Example starter JavaScript for disabling form submissions if there are invalid fields
|
||||||
(function () {
|
(function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
@@ -118,7 +119,6 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
|
|||||||
}, false);
|
}, false);
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
</script>
|
|
||||||
{{< /example >}}
|
{{< /example >}}
|
||||||
|
|
||||||
## Browser defaults
|
## Browser defaults
|
||||||
|
Reference in New Issue
Block a user