Files
PIbd-21_Permyakov_R.G._IP/js/validation.js

21 lines
877 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export default function validation() {
// поиск всех форм с классом .needs-validation
const forms = document.querySelectorAll("form.needs-validation");
forms.forEach((form) => {
form.setAttribute("novalidate", "");
form.addEventListener("submit", (event) => {
// выключить стандартное действие
event.preventDefault();
// предотвращает распространение preventDefault
// на другие объекты
event.stopPropagation();
if (!form.checkValidity()) {
// добавляет к форме класс was-validated
form.classList.add("was-validated");
} else {
form.classList.remove("was-validated");
}
});
});
};