console.log("JS works properly");
const vname = document.getElementById("vname");
const nname = document.getElementById("nname");
const idnum = document.getElementById("idnum");
const psword = document.getElementById("psword");
const form = document.getElementById("form");
const ErrorElements = document.getElementById("error");
form.addEventListener("submit", (e) => {
let messages = [];
if (vname === "" || vname == null) {
messages.push("Sie haben kein Vorname eingegeben.");
}
if (messages.length > 0) {
e.preventDefault();
ErrorElements.innerText = messages.join(", ");
}
});
So, I’m simply trying to get a form validation with this code, but when I click on the submit button, the e.preventDefault() just doesn’t seem to do its work (I have left all values empty). This code is part of a project for school.
Thanks in advance 🙂
New contributor
Armin-Rafael Zdroba-Fisteag is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3