[英]IF statement validate.js
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. 我需要使用validate.js来验证表单中的字段,在尝试使用if语句之前,表单和验证工作正常。
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. 这可行,但是如果我提交表单时没有单击
input[type="radio"]
,那么它将完全无法验证。
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();
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.