jQuery validate less than

后端 未结 5 1788
盖世英雄少女心
盖世英雄少女心 2020-12-15 12:05

I am trying to write a Less than validator for jQuery.

I want to compare one text box against another, so if I have:



        
5条回答
  •  执念已碎
    2020-12-15 12:17

    I think you can do that without actually writing your own validator method.

    $('#myForm').validate({
        rules: {
             value1: {
                 maxlength: $('#value2').val().length
             }
        }
    });
    
    $('#value2').change(function() {
        $('#value1').rules('remove', 'maxlength').rules('add', {
             maxlength: $('#value2').val().length
        });
    });
    

    or even better without code duplication

    function getRule() {
        return {
            maxlength: $('#value2').val().length
        };
    }
    
    $('#myForm').validate({
        rules: {
             value1: getRule()
        }
    });
    
    $('#value2').change(function() {
        $('#value1').rules('remove', 'maxlength').rules('add', getRule());
    });
    

提交回复
热议问题