Guys, how are you? I have a doubt, I have to validate a field type text of a form, the issue is that I generate a form for each row of a table that I bring by jsp. so:
<tbody>
<c:forEach items="${listaRegalosSinElegir}" var="l">
<form action="AltaRegalos" method="Post" onsubmit="return validar()"> <tr><td><p>${l.denominacion}</p></td><td><p>${l.denominacion}</p></td><td><p>${l.precioUnitario}</p></td><td><p><input type="number" name="Cantidad" class="CampoForm"><input type="hidden" value="<c:out default="" value="${idPareja}"></c:out>" name="IdParejaListado" ><input type="hidden" name="idRegalo" value="${l.id}"></p></td><td><input type="submit" name="AgregarRegalo" value="Agregar" class="btn btn-success btn-sm"></td></tr> </form>
</c:forEach>
</tbody>
I must validate that the input field of the form to which I click on l submit, is not empty, I know how to do it when there is only 1 form, but in this case the quantity is large. from that submit I call "return validate ()" from onsubmit.
This is the script that I have so far:
let MensajeError= document.getElementById("MensajeError");
let Combos= document.getElementsByClassName("selector");
let Campos = document.getElementsByClassName("CampoForm");
function validar(){
for (var i in Campos) {
if(Campos[i].value===""){
Campos[i].style.borderColor="red";
Campos[i].focus();
MensajeError.innerHTML="Debe Completar todos los campos.";
return false;
}
else{
Campos[i].style.borderColor="green";
}
}
for (var i in Combos) {
console.log(Combos[i].value);
if(Combos[i].value===0){
Combos[i].style.borderColor="red";
Combos[i].focus();
MensajeError.innerHTML="Debe Seleccionar una opcion.";
return false;
}
else{
Combos[i].style.borderColor="green";
}
}
return true;
}
I hope you understand. I have to do it with pure javascript, I know that JQUERY is better, but good. It must be in Vanilla Javascript. You help me?