Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically setting min value on textfield using Jquery Validate plugin

I'm trying to have a dynamically updated min value on one field, depending on input from other fields. Heres my code in short:

$("#new_project").live("click", function() {

    switch($('input:radio[name=quality-level]:checked').val()){
        case 'average' : ppw = .006;
        case 'below-average' : ppw =.004;
        case 'good' : ppw = .008;
        case 'very-good' : ppw = .016;
    }

    if ($('#minimum-word-length').val() && $('input:radio[name=quality-level]:checked').val())
    {
        min_price = $('#minimum-word-length').val() * ppw;

    }

    $("#new_project, .edit_project").validate({
    rules: {
    ...
    "price-per-article": {required: true, number: true,  min:min_price},
    ...
    },
    errorPlacement: function(error, element) { }


    });

});

Min price is set correctly, and updates correctly. That said, for whatever reason, the min value rule doesnt update which i think is because the validate code is only loaded on document load. So I guess is there a way to reload the rules so that the min value changes when then the two necessary fields are filled?

Thanks!

like image 449
Dave G Avatar asked Dec 15 '10 17:12

Dave G


1 Answers

The problem is you're setting up the validate object with the price-per-article on initial load. I'm not familiar with the validate plug-in, but in general, if your properties change for an object real time, you'll want to use a callback function for that property as opposed to setting it's data on load.

So for the price-per-article entry it'd probably look like:

"price-per-article": {
  required: true,
  number: true,
  min: function () { return $('#minimum-word-length').val() * ppw; }
}
like image 184
PriorityMark Avatar answered Oct 12 '22 23:10

PriorityMark