简体   繁体   中英

JQuery Validation - Limit number of multiple file uploads method

I am using JQuery Validation and in my form I have 4 multiple file upload input fields. I would like to set a limit to the 1st 3 fields to have a maximum of 3 files and the final field to have a maximum of 5.

Ideally I would like to create a new method so that I can just set rules for each of the fields in case they need amending in the future.

Unfortunately my JQuery knowledge (and coding skills in general) is very low and was wondering if someone would be able to point me in the right direction with what I'm doing wrong with what I've created below.

maxupload: function( value, element, param ) {
    return this.optional( element.files.length ) || value <= param;
},

On my website I've created the following:

$("#survey").validate({
    rules: {
        "outsideImg[]": {
            required: true,
            maxupload : 3,
            }
    },
    messages: {
            "outsideImg[]": {
                required: "You must upload at least 1 image (maximum of 3)",
                maxupload: "You can only upload a maximum of 3 images",
            },
    }
});

If there are no files selected it works fine and returns the correct message. The maximum limit is not working.

Any help would be greatly appreciated.

I've solved it... Rather than delete thought I'd leave here so that others facing the issue can see the resolution.

maxupload: function( value, element, param ) {
    var length = ( element.files.length );
    return this.optional( element ) || length <= param;
},

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