var inputs = document.querySelectorAll(".form__input");
var inputsID = [nameInput, nubmerInput, dateInput];
var form = document.querySelector(".feedback__form");
form.addEventListener("submit", e => {
e.preventDefault();
for(elem of inputsID) {
var validated = inputValidate(elem);
}
if (!validated) {
console.log("Validation error!");
} else {
console.log("Validation succes!");
console.log("Form submited");
form.submit();
}
});
function inputValidate (id) {
let validated = true;
let errorInit = (elem) => {
elem.classList.add("form__input_error");
elem.parentNode.classList.add("input-wrapper_error");
if (elem.type != "date") {
elem.placeholder = "Format error*";
}
}
let errorUnInit = (elem) => {
elem.classList.remove("form__input_error");
elem.parentNode.classList.remove("input-wrapper_error");
}
if (id.name === "name") {
if (id.value.trim() === "") {
validated = false
errorInit(id);
id.addEventListener("focus", () => {
errorUnInit(id);
id.placeholder = "Name*";
})
}
} else if (id.name === "phone_number") {
if (id.value.length != 20) {
validated = false
errorInit(id);
id.addEventListener("focus", () => {
errorUnInit(id);
id.placeholder = "+420 (000) 000-00-00*";
})
}
} else if (id.name === "date") {
if (id.value.trim() === "") {
validated = false
errorInit(id);
id.addEventListener("focus", () => {
errorUnInit(id);
})
}
}
return validated
};