I’m using Javascript to submit a form as I’m also using reCaptcha v3. I understand (correct me if I’m wrong) the required atterribute doesn’t work when submitting a form via JS.
Therefore my required fields are not longer being validated and that’s the reason my form is being submitted without data in these required fields.
I’m doing something like this to overcome this.
If at the end of my JS validation ‘outcome’ is still 0 then the form submits.
My questions are
1. This is very long script as I have 20 fields (stree address, town, city, zip, name, email, confirm email etc etc) Is there a shorter way of doing this?2. Is this the best way3. Am I correct with why my required fields are not working?
“`
let outcome = 0;
if (document.getElementById(“email”).value == “”) {
document.getElementById(“emaillabel”).innerHTML = “Please complete Email”;
document.getElementById(“emaillabel”).style.color = “red”;
outcome = 1;
} else {
document.getElementById(“emaillabel”).style.color = ‘black’;
}
if (outcome == 0){
document.getElementById(“myform”).submit();
}