jquery validation plugin - file upload

我的未来我决定 提交于 2019-12-25 06:49:30

问题


I am using jquery validation plugin for validating my form. I have one file field in form for uploading documents.

<input type="file" name="policyBriefFiles" id="policyBriefFiles" multiple="multiple">

Here is my validation code for this field.

policyBriefFiles: {
         extension:'xlsx,pdf',
         filesize: 2097152,  
     },

Now I want users to select maximum two files, but with multiple attribute specified with file user can select any number of files.

How do I restrict users to select maximum two files and no more? Is there any rule available in jquery validation plugin for this? (like maxFile: 2)

I don't mind any other workaround also.


回答1:


I made custom validation method for this. Here it is

$.validator.addMethod("maxFilesToSelect", function(value, element, params) {
    var fileCount = element.files.length;
    if(fileCount > params){
        return false;
    }
    else{
        return true;
    }
},  'Select no more than 2 files');

And specify rule as

policyBriefFiles: {
         extension:'xlsx,pdf',
         filesize: 2097152,  
         maxFilesToSelect : 2,
     },

Working as charm :-)



来源:https://stackoverflow.com/questions/36147958/jquery-validation-plugin-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!