jQuery Validate, out of two blank fields, at least one field must be filled or both

后端 未结 2 939
野趣味
野趣味 2020-12-16 03:37

I want to validate my form so that is out of two blank fields, at least one field must be filled and two fields also can be filled; but can\'t leave any field blank.

2条回答
  •  無奈伤痛
    2020-12-16 03:50

    You are attempting to use validator.addMethod which is part of the jQuery Validate plugin. You'll need to include this plugin in your code if you haven't already.

    Then use the require_from_group rule that's already part of the Validate plugin's additional-methods.js file. (Don't forget to include the additional-methods.js file too.)

    rules: {
        myfieldname: {
            require_from_group: [1, ".class"]
        }
    }
    
    • First parameter is the number of items to be required.
    • Second parameter is the class assigned to all elements in your grouping. I added a send class to your two input elements.

    • Also use the groups option to consolidate the two messages into one.

    jQuery:

    $(document).ready(function () {
    
        $('#forgot_pass_form').validate({ // initialize the plugin
            groups: {  // consolidate messages into one
                names: "uname email"
            },
            rules: {
                uname: {
                    require_from_group: [1, ".send"]
                },
                email: {
                    require_from_group: [1, ".send"]
                }
            }
        });
    
        //  for your custom message
        jQuery.extend(jQuery.validator.messages, {
            require_from_group: jQuery.format("'Please enter either username/ email address to recover password'/Please fill out at least {0} of these fields.")
        });
    
    });
    

    Working Demo: http://jsfiddle.net/sgmvY/1/


    EDIT: As per Github, there is an open issue with the require_from_group method. Until it's fixed, the developer is recommending this solution below. Since you would manually add the revised method into your code, there is no need to include the additional-methods.js file.

    New Working Demo: http://jsfiddle.net/kE7DR/2/

    $(document).ready(function () {
    
        jQuery.validator.addMethod("require_from_group", function (value, element, options) {
            var numberRequired = options[0];
            var selector = options[1];
            var fields = $(selector, element.form);
            var filled_fields = fields.filter(function () {
                // it's more clear to compare with empty string
                return $(this).val() != "";
            });
            var empty_fields = fields.not(filled_fields);
            // we will mark only first empty field as invalid
            if (filled_fields.length < numberRequired && empty_fields[0] == element) {
                return false;
            }
            return true;
            // {0} below is the 0th item in the options field
        }, jQuery.format("'Please enter either username/ email address to recover password'/Please fill out at least {0} of these fields."));
    
        $('#forgot_pass_form').validate({ // initialize the plugin
            groups: {
                names: "uname email"
            },
            rules: {
                uname: {
                    require_from_group: [1, ".send"]
                },
                email: {
                    require_from_group: [1, ".send"]
                }
            }
        });
    
    
    
    });
    

提交回复
热议问题