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>
1
  • read this Commented May 28, 2012 at 10:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It still works just for the first file input in my case. Other two ("fotografija1, fotografija2") are not validated. :/
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.
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/
-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();
    },

1 Comment

I have tried to modify the validate.js but it was not working for me

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.