简体   繁体   中英

jquery validate input array and passing dynamic values

I'm trying to validate a group of input arrays using jquery validator plugin. I'm having problems understanding how to pass another element into the validation.

My code so far:

$('#edit-fixtures').validate({
            rules: {
                "player_a[]": {
                    required: true,
                    uniqueMatch: function () {
                        return $(this).next('.player').val();
                    },
                },
                "player_b[]": {
                    required: true,
                    uniqueMatch: function () {
                        $(this).closest('.player').val();
                    },
                },
            }
        });

My input elements are a series of paired select boxes named player_a[] player_b[] There are about 40 pairs. Each pair should be unique and that's what i'll be validating.

I'm trying to pass the value of the nearest player_b to the changed player_a and vice versa.

I have a change method to validate on each change:

$(".player").change(function () {

            var check = $(this).valid();

            console.log("validation:", check);

        });

I'm still working on the validation method but cant seem to get the parameters to the method correctly.

Is there a way of doing this?

uniqueMatch: [$item1, $item2, ....]

$.validator.addMethod('uniqueMatch', function(value, element, parameterValue) {
    var item1Value = $(parameterValue[0]).val();
    //and so on...
    //Based on values, you return true or false;
    //Voila!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM