简体   繁体   English

jQuery验证-限制多个文件上传的方法

[英]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. 我正在使用JQuery Validation,在我的表单中,我有4个多个文件上传输入字段。 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. 我想为第一个3个字段设置一个限制,以最多包含3个文件,而最后一个字段设置为最大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. 不幸的是,我的JQuery知识(以及一般的编码技能)非常低,并且想知道是否有人可以将我在下面创建的内容做错的事情指向正确的方向。

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;
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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