简体   繁体   English

jQuery验证方法-验证

[英]jQuery Validation Method - validation

Having problem to disable validation in idealforms, I would like to have following fields not being validated 在禁用Idealforms中的验证时遇到问题,我希望以下字段未通过验证

postcode3, memo2, previous_year, previous_month 邮递区号3,备忘录2,上一个年度,上一个月

until value in current_year is less then 3 直到current_year中的值小于3

$(document).ready(function(){

$('#current_year').on("input", function() {
    var dInput = this.value;
    console.log(dInput);
    $('#current_year').text(dInput);

    $('form.idealforms').idealforms('addRules', {
        'postcode3': ( parseInt( $('#current_year').val() ) < 3) ? 'required' : '',
    });
});



      $('form.idealforms').idealforms({

      silentLoad: false,    

      rules: {
        'postcode2': 'required',
        'memo1': 'required',
        'current_year': 'required',
        'current_month': 'required',
      },


      onSubmit: function(invalid, e) {
        e.preventDefault();
        $('#invalid')
          .show()
          .toggleClass('valid', ! invalid)
          .text(invalid ? (invalid +' invalid fields') : 'All good!');
      }
});

Assuming the rest of your code is correct I think the issue is that val() returns a string and not a number . 假设您其余的代码是正确的,我认为问题是val()返回的是string而不是number You'll need to write your expressions like so 您需要像这样编写表达式

( parseInt( $('#current_year').val() ) < 3 ) : 'required' : '';

or compare val() to the string value of the number. 或将val()与数字的字符串值进行比较。

( $('#current_year').val() < '3' ) : 'required' : '';

Edit 编辑

There is a second issue in that once your rules are declared, nothing in your code changes the rules. 第二个问题是,一旦声明了规则,代码中的任何内容都不会更改规则。 So if onload $('#current_year').val() is less then 3, your fields will always be required. 因此,如果onload $('#current_year').val()小于3,则始终需要输入字段。

According to the plugin docs you can add rules after initializing the plugin like so 根据插件文档,您可以在初始化插件后添加规则,如下所示

$('form').idealforms('addRules', {
  'comments': 'required minmax:50:200'
});

So you might be able to do use this to update the rules like so. 因此,您也许可以使用它来更新规则。

$('#current_year').on("input", function() {

    $('form.idealforms').idealforms('addRules', {
        'postcode3': ( parseInt( $('#current_year').val() ) < 3) ? 'required' : '',
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM