I am new to programming and am trying to build this form validation function to check the validity of different input types and throw relevant errors. The part I am struggling with is incorporating iteration so that it runs through my array of different input types and checks their validity.
I do not know if this is the most effective way of doing it but this is all I could think of for now.
This is the code I am working on and at the moment it only works with the input type ’email’.
What I thought I could do is add a loop function to iterate through my inputTypes array and check validity (perhaps with checkValidity()?) for each of the input types listed and if invalid then send it over to the showError function.
However, where and how can I fit it in so that it works with the dot notation?
Should it be within the event listener or the showError function or entirely separate?
I would be very grateful for any hints or critiques.
const form = document.querySelector("form");
const email = document.querySelector('input[type=email]');
const tel = document.querySelector('input[type=tel]');
const text = document.querySelector('input[type=text]');
const radio = document.querySelector('input[type=radio]');
const inputTypes = [email,tel,text,radio];
const emailError = document.querySelector('input[type=email] + span.error');
//detect when user stops typing and check if content is valid
email.addEventListener("keyup", (event) =>{
if (email.validity.valid) {
emailError.textContent = "";
emailError.className = "error";
}
else {
setTimeout(() =>{
showError();
},2000);
}
});
//prevent form from being submitted if the value isn't valid
form.addEventListener("submit", (event) => {
if (!email.validity.valid) {
showError();
event.preventDefault();
}
});
//match appropriate error messages with relevant constraint violations of an input type
function showError() {
if (email.validity.valueMissing) {
emailError.textContent = "You need to enter an email address.";
} else if (email.validity.typeMismatch) {
emailError.textContent = "Entered value needs to be an email address.";
} else if (email.validity.tooShort) {
emailError.textContent = `Email should be at least ${inputTypes.minLength} characters; you entered ${inputTypes.value.length}.`;
}
emailError.className = "error active";
}
2
Answers
Why not just make it required, then you get the HTML5 input validation without having to code it yourself?
Only drawback I see right now, is that name@somewhere is seen as valid in the built-in test
You can use the
invalid
event on the form to display feedback to the user. Then, when the user starts to type you can evaluate the input usinginput.validity.valid
.In the case of the radio buttons both are valid is one is checked. This is a special case in the
input
event listener. If more radio buttons have the same name (a group) they are all valid if one is valid, so theinvalid
class name has to be removed from all of them.