2

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.

1 Answer 1

5

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;
},
Sign up to request clarification or add additional context in comments.

1 Comment

hi, Can you please tell me what parameters to pass if I dont want user to add more than 3 files and where to pass it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.