Jquery Validate dropdown required if field empty

后端 未结 2 409
野趣味
野趣味 2020-12-30 05:00

I\'m trying to make the dropdown required if the #category field is empty.

Thanks!

JQUERY ATTEMPT #1:

$(\"#uploadDocsForm\").validate({
    r         


        
2条回答
  •  感情败类
    2020-12-30 05:53

    $("#uploadDocsForm").validate({
        rules: {
            name: {
                required: true,
                minlength: 2,
                maxlength: 255  
            },
            cat_id: {
                required: {
                    depends: function(element) {
                        return $("#category").val() == '';
                    }
                }
            }
        },
        messages: {
            name: 'Please enter a Document Name.',
            cat_id: 'Please select a Category.'
        }
    });
    

    The depends function now checks if the category element is filled, and if so the second is required. Otherwise it is optional (meaning can be filled).

    Use Cases:

    • Category filled, cat_id empty : invalid
    • Category filled, cat_id filled : valid
    • Category empty, cat_id empty : valid
    • Category empty, cat_id filled : valid

提交回复
热议问题