Conditional Rule Validation in JQuery

后端 未结 1 859
失恋的感觉
失恋的感觉 2021-01-02 23:59

I am using JQuery Validation. Now I want my rules to be invoked only when certain condition is satisfied, namely, I want $(\"#AppSelectField\").is(\':hidden\')

相关标签:
1条回答
  • 2021-01-03 00:36

    This should do it:

    $(function() {
        var checkIfFieldVisible = function() {
            return !$("#AppSelectField").is(':hidden');
        };
    
        $("#RequestLock").validate({
            rules: {
                FloorNum: {
                    required: { depends: checkIfFieldVisible },
                    digits: { depends: checkIfFieldVisible }
                }, // <-- EXTRA COMMA
            },
            message: {
                FloorNum: "The floor number must be integer",
            }, // <-- EXTRA COMMA
        });
    });
    

    (I marked some extra commas you have on your code, as they will make IE choke)

    0 讨论(0)
提交回复
热议问题