-->

Setting Input Validation Range Dynamically in JQue

2019-07-21 09:26发布

问题:

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.

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

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>

Thanks!

回答1:

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"
                 }                             
    }); 
}