在JQuery中动态设置输入验证范围 [英] Setting Input Validation Range Dynamically in JQuery

查看:144
本文介绍了在JQuery中动态设置输入验证范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预先感谢您关注此内容. :-)

Thank you in advance for looking at this. :-)

表单验证本身可用于其他仅需填写字段的项目-没问题.

The form validation itself works on other items where the field is simply required - that is no problem.

我正在尝试从我的自动填充动态设置一个数字范围来进行验证.
我在此处找到了bassistance.de JQuery验证.

I am trying to set a numeric range for validation from my autocomplete dynamically.
I am using the bassistance.de JQuery validation found here.

选择后,我正在调用一个函数,但是需要将其添加到.validate()代码中,而不是自己的函数中(我认为),但是现在确定如何组合它们. 自动完成功能是一个通用功能,可以由多个输入调用.

Upon the select, I am calling a function, but it needs to be added to .validate() code instead of its own function (I think), but now sure how to combine them. The autocomplete is a generic function that is called by multiple inputs.

<script>
   $().ready(function() {
       // validate the form when it is submitted
       $("#form4100").validate();
   });
</script>
<script>
function Autocomplete(numberLocation,nameLocation,dscLocation,chargeLocation,amountLocation) {
    select: function( event, ui ) {          
            $(numberLocation).val( ui.item.value );
            $(nameLocation).html( ui.item.desc );                     
            alert("Minimum Investment: "+ui.item.minimum);
            setvalidation(amountLocation,ui.item.minimum);
            return false;
    }        
}

function setvalidation(amountLocation,minimum){
    alert("validation function launched");
    amountLocation.validate({
      rules: {
        field: {
          required: true,
          range: [minimum, 5000000]
        }
      }
    });    
}
</script>

谢谢!

推荐答案

我发现(在进一步阅读验证插件上的文档之后)有一个.rules add方法.因此.select可以调用此函数:

I found out (after reading the docs further on the validation plugin) that there is a .rules add method. So the .select can call this function:

function setvalidation(amountLocation,minimum){
    amountLocation.rules("add",{
                  required: true,
                  range: [minimum, 5000000],
                  messages: {
                      range: "$ "+minimum + " Minimum"
                 }                             
    }); 
}

这篇关于在JQuery中动态设置输入验证范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆