If i remove the standard required attribute from the HTML input element, it freezes the whole form. How do i fix this to only display the error message with ID=“error” ?
I basically would like to have the standard validation error not to show. Instead it should show the error message lined out in the JS file on line 54, placed inside the validateForm() function.
Yes I understand, but I cannot understand the freez part you mentioned
You may please explain the freez?!
Please note once you set the value for that error tag, you will never hide or clear it when user added data correctly(no error).
You keep the validity of the form using valid variable, before you return from validate form function, check if form is valid, then hide the error message(or clear it)
Sorry man, English is not my first language, lol not even my second or third
What i meant is that it does not show the error i would like (on line 54 in JS file). Also the highlighted input fields (if input == “”) are not showing anymore after removing that line. It flickers on/off for a m/s and does not show any error message at all. I have now left the required lines to stay in the element because it also throws another error upon testing with:
You keep the validity of the form using valid variable, before you return from validate form function, check if form is valid, then hide the error message(or clear it)
I think it should have another else if statement there to check but can’t seem to work around the existing lines:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value == "") {
// add an "invalid" class to the field:
y[i].className += " invalid";
//display error message
document.getElementById("error").innerHTML = "Input required";
// and set the current valid status to false:
valid = false;
}
}
I notice that you have put a div with id=“error” below each of your form input elements. I think you mean to use class=“error”. id is meant to be used on only one unique element whereas class is meant to identify one or more similar elements.
Probably not the cleanest way to traverse DOM but it works. You might work out something simpler.
function validateForm() {
// This function deals with validation of the form fields
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
//y = x[currentTab].getElementsByTagName("input");
y = x[currentTab].querySelectorAll("input[required]")
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
let output = ''; //default
// If a field is empty...
if (y[i].value == "") {
// add an "invalid" class to the field:
y[i].className += " invalid";
//display error message
output = "Input required"
// and set the current valid status to false:
valid = false;
}
// after checking for empty update error div
let target = y[i].parentNode.getElementsByClassName("error")[0];
target.innerHTML = output;
console.log("hit target")
}
// If the valid status is true, mark the step as finished and valid:
if (valid) {
document.getElementsByClassName("step")[currentTab].className += " finish";
}
return valid; // return the valid status
}
You still have issues with that error field expanding and collapsing so your form jumps some. You might do better to change visibility:hidden with a className change instead of changing innerHTML