generated from tripleten-com/se_project_aroundtheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86a49b3
commit c8230a9
Showing
4 changed files
with
136 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
function showInputError(formEl, inputEl, { inputErrorClass, errorClass }) { | ||
const errorMessageEl = formEl.querySelector("#" + inputEl.id + "-error"); | ||
inputEl.classList.add(inputErrorClass); | ||
errorMessageEl.textContent = inputEl.validationMessage; | ||
errorMessageEl.classList.add(errorClass); | ||
} | ||
|
||
function hideInputError(formEl, inputEl, { inputErrorClass, errorClass }) { | ||
const errorMessageEl = formEl.querySelector("#" + inputEl.id + "-error"); | ||
inputEl.classList.add(inputErrorClass); | ||
errorMessageEl.textContent = ""; | ||
errorMessageEl.classList.remove(errorClass); | ||
} | ||
|
||
function checkInputValidity(formEl, inputEl, options) { | ||
if (!inputEl.validity.valid) { | ||
showInputError(formEl, inputEl, options); | ||
} else { | ||
hideInputError(formEl, inputEl, options); | ||
} | ||
} | ||
|
||
function setEventListeners(formEl, options) { | ||
const { inputSelector } = options; | ||
const inputEls = [...formEl.querySelectorAll(inputSelector)]; | ||
inputEls.forEach((inputEl) => { | ||
inputEl.addEventListener("input", (e) => { | ||
checkInputValidity(formEl, inputEl, options); | ||
}); | ||
}); | ||
} | ||
|
||
function enableValidation(options) { | ||
const formEls = [...document.querySelectorAll(options.formSelector)]; | ||
formEls.forEach((formEl) => { | ||
formEl.addEventListener("submit", (e) => { | ||
e.preventDefault(); | ||
}); | ||
|
||
setEventListeners(formEl, options); | ||
}); | ||
} | ||
|
||
const config = { | ||
formSelector: ".modal__form", | ||
inputSelector: ".modal__input", | ||
submitButtonSelector: ".modal__button", | ||
inactiveButtonClass: "modal__button_disabled", | ||
inputErrorClass: "modal__input_type_error", | ||
errorClass: "modal__error_visible", | ||
}; | ||
|
||
enableValidation(config); |