1

How can I validate an array of file input with jquery.validate plugin?

The code works fine with single file, but not with multiple upload, where name="" is an array.

jQ:

<script type="text/javascript">
    $(document).ready(function(){
        $("#Main").validate({
            rules: {
                pic: { required:true, accept: "jpg|jpeg" }
            }}
        );
    });
</script>

Single upload:

<html>
    <head>
    </head>
    <body>
        ...
        <td>Fotografija 1:</td>
        <td> <input type="file" class="fup" name="pic" /> </td>
        ...
    </body>
</html>

Multiple upload

<html>
    <head>
    </head>
    <body>
        ...
            <td>Fotografija 1:</td>
            <td> <input type="file" class="fup" name="pic[]" /> </td>
        </tr>
        <tr>
            <td>Fotografija 2:</td>
            <td> <input type="file" class="fup" name="pic[]" /> </td>
        </tr>
        <tr>
            <td>Fotografija 3:</td>
            <td> <input type="file" class="fup" name="pic[]" /> </td>
        ...
    </body>
</html>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
John
  • 1,619
  • 8
  • 24
  • 34

2 Answers2

0

You can try this:

 $("input.fup").each(function(){
       $(this).rules("add", {
           required:true, 
           accept: "jpg|jpeg"
       });                    
 });

For detail see here

Here is another resource.

Community
  • 1
  • 1
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • 3
    It still works just for the first file input in my case. Other two ("fotografija1, fotografija2") are not validated. :/ – John May 28 '12 at 11:08
  • 1
    It only validates the first input when the `name` attribute is used multiple times. This plugin requires that naming be unique; there is no workaround! The first question you linked to in your answer is about editing the core of the plugin itself. – Sparky Sep 17 '18 at 14:48
  • 1
    Answer should be fixed or deleted. Your first link says to edit the plugin itself, which only proves this cannot be done with this plugin. The second link shows unique names, which is not what the OP is doing here. Finally, read the first comment on this answer above... proof: [your solution just does not work when field name is repeated. https://jsfiddle.net/gbxnadwo/](https://jsfiddle.net/gbxnadwo/) – Sparky Jul 30 '19 at 18:31
-2

Have some issue here

"pic[]" {your rules}

and

rules("add) 

wokring only for first element in array.

===

ok i found out solution: you should fix jquery.validate.js

Find checkForm function

and replace it to:

        checkForm: function() {
        this.prepareForm();
        for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
            if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
                for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
                    this.check( this.findByName( elements[i].name )[cnt] );
                }
            } else {
                this.check( elements[i] );
            }
        }
        return this.valid();
    },
Maxim Pokrovskii
  • 353
  • 4
  • 11