jQuery Validate Plugin: how to validate a dropdown

前端 未结 3 1736
栀梦
栀梦 2021-02-06 15:12

I\'m trying to use the jQuery Validate plugin to validate a dropdown. It validates the rest of my form correctly. But it doesn\'t work on the dropdown.

Here is my jQue

3条回答
  •  [愿得一人]
    2021-02-06 16:04

    If for some reason you can't set the value to blank like Spark said, you can just add your own JQuery validation function instead of the default required preset.

    $.validator.addMethod("aFunction",
        function(value, element) {
            if (value == "none")
                return false;
            else
                return true;
        },
        "Please select a value");
    
    $('#campaignForm').validate({
        rules: {
             campaign_name: {
                 required: true
             },
             html_url: {
                 aFunction: true
             }
        },
        messages: {
            campaign_name: 'Please enter a campaign name',
            html_url: 'Please select a template'
        }
    });
    

提交回复
热议问题