I coded a form with a hidden field, if the user selects "telephone" via radio box, another field is shown.
I need to validate the fields in the form using validate.js, The form and validate were working before I tried with an if statement.
The code:
$(document).ready(function(){
$('#info_tel').hide();
$('input[type="radio"]').click(function(){
if($(this).attr("value")=="2"){
$("#info_tel").show();
$("#form1").validate({
rules: {
nombre: "required",
apellido1: "required",
email: {
required: true,
email: true
},
terminos: "required",
telefono: "required"
},
messages: {
nombre: "*Requerido.",
apellido1: "*Requerido.",
email: "*Requerido.",
terminos: "*Requerido."
}
});
}
if($(this).attr("value")=="1"){
$('#info_tel').hide();
$("#form1").validate({
rules: {
nombre: "required",
apellido1: "required",
email: {
required: true,
email: true
},
terminos: "required"
},
messages: {
nombre: "*Requerido.",
apellido1: "*Requerido.",
email: "*Requerido.",
terminos: "*Requerido."
}
});
}
});
});
This works, but if I submit the form without clicking the input[type="radio"]
, it does not validate at all.
I did it. The full code:
$(document).ready(function() {
$("#form1").validate({
rules: {
nombre: "required",
apellido1: "required",
email: {
required: true,
email: true
},
terminos: "required"
},
messages: {
nombre: "*Requerido.",
apellido1: "*Requerido.",
email: "*Requerido.",
terminos: "*Requerido."
}
});
$('#info_tel').hide();
$('input[type="radio"]').click(function(){
if($(this).attr("value")=="2"){
$("#info_tel").show();
//tel required
$( "#telefono" ).rules( "add", {
required: true,
minlength: 10,
messages: {
required: "*Requerido.",
minlength: jQuery.format("{0} digitos")
}
});
}
if($(this).attr("value")=="1"){
$('#info_tel').hide();
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.