Dear All,
I am making a normal form with name, rut and surname.
I am sending the form with a button, but when the data is not validated, the form is still sent. so my question is.
Is there a javaScript function that stops the submit button when validation fails?
function sendForm(){
alert("ENTRO SEND");
alert("valido: "+valido);
if(valido){
alert("valido TRUE");
validateSetDataAhorro();
}else{
alert("valido FALSE");
return false;
}
}
<form action="http://algunsitio.com/prog/usuarionuevo" method="post">
<p>
<label for="nombre">Nombre: </label>
<input type="text" id="nombre"><br/>
<label for="apellido">Apellido: </label>
<INPUT type="text" id="apellido"><br/>
<label for="email">Rut: </label>
<INPUT type="text" id="email"><br/>
<button onclick="javascript:sendForm();" class="btn btn-second"
title="Continuar">Boton</button>
</p>
</form>
If you leave the button out of the form it will not send it, then you validate the fields (I have not done any validation), if the variable
valido
is correct it will send the form withsubmit()
javascript, otherwise it shows an alert for you to validate the fields. Try changing the variable fromfalse
totrue
to see how it works for you.The logic you are driving is fine. You have a submit button and in the click event
You call the sendForm() function.
The problem is that the valid variable is not defined, how do you know that the form is valid? It may be that it only has values (if so, you can validate it with required in the input).
You must work the valid variable, which you are going to validate and that way you would only be sending the form when the form data has been validated on the client.
A basic way to validate if the inputs have value is
In this code, the browser validates that the input field has a value, if so, it proceeds to send it, otherwise it requests it.
Cheers,
I don't see any submit button, so you don't need anything to cancel.
You actually have a normal button that makes a javascript call. In the correct case, said javascript will have to do the submit of the form, or an Ajax call or whatever corresponds.