I would like to know how to validate a Checkbox
with JQuery
in a small form.
<form action="">
<a>Correo: </a>
<input type="email" name="correo">
<br>
<a>Telefono: </a>
<input type="text" name="telefono">
<br>
<a>Aceptar condiciones: </a>
<input type="checkbox" name="aceptar">
<br>
<button type="submit" id="submit">Enviar Formulario</button>
</form>
I found several types of validations .is(selector)
and .prop()
but I tried to apply them and I couldn't get it, I just want it to throw an alert that says something like Accept the conditions .
When you use you
is()
have to use the selector:checked
:When the conditions have not been marked, you must use
event.preventDefault()
to prevent the form from doing thepost
.You could implement something like
There are also other alternatives such as
It only asks if it is checked with
.is(':checked')
:Live DEMO
You can make use of "required" and thus you don't have to use JavaScript, although the displayed message will depend on the browser.
<input type="checkbox" name="aceptar" required>