Dynamically setting a minimum value in a text field using the Jquery Validate plugin

I am trying to get a dynamically updated value of min in one field, depending on the input from other fields. Here is my code shorter:

$("#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) { }


    });

});

The minimum price is set correctly and updated correctly. However, for some reason, the minimum value rule does not update, which I believe is because the verification code only loads when the document loads. So I guess there is a way to reload the rules, so that the min value changes when the two required fields are populated?

Thank!

+3
source share
3 answers

, validate . validate, , , , .

, :

"price-per-article": {
  required: true,
  number: true,
  min: function () { return $('#minimum-word-length').val() * ppw; }
}
+5

, , :

"price-per-article": {
    required: true,
    number: true,
    min: function() { return min_price; }
}

, min_price , .

+1

min year max year min:

$(document).ready(function () {
    function getMinYear() {
        var d = new Date();
        var y = parseInt(d.getFullYear());

        console.log("Min: " + y);
        return y;
    }

    $("#frmAddCard").validate({
        rules : {
            year : {
                required : true,
                number: true,
                min: getMinYear(),
                max: getMinYear() + 10
            }
        },
        messages : {                
            year : {
                required : "Expiration Date: Please enter valid year (e.g. 2016)",
                number : "Expiration Date: Please enter valid year (e.g. 2016)",
                min : function (elem) {
                    var y = getMinYear();

                    return "Min: Please enter the value between " + y.toString() + " and " + (y + 10).toString();
                },
                max : function (elem) {
                    var y = getMinYear() + 10;

                    return "Max: Please enter the value between " + y.toString() + " and " + (y + 10).toString();
                }
            }
        },
        submitHandler : function (form) {
        },
        invalidHandler : function (event, validator) {              
        }
    });
});
0

Source: https://habr.com/ru/post/1780493/


All Articles