1

I am trying to validate a multiple file input. I have rules for it. However the very basic thing of required: true is not working. Here is my code..

        <form id="profile-form" action="" method="post">

            <div class="row">
            <div class="form-group col-md-2">
                <label style="padding-left:20px;padding-top:10px">Upload image<strong> (5 images required)<strong></label>
            </div>
            <div class="form-group col-md-4">
                <input class="form-control" type="file" name="files[]" id="files" accept="png|jpg|jpeg" required="required" multiple>
            </div>
        </div>
</form>

The validation.js containing the rules are as follows:

     $("#profile-form").validate({
    rules: {

      name: {
        required: true,
        nowhitespace: true,
        lettersonly: true
      },

 'files[]': {
            required: true,
          },    
    },

    messages: {

      name: {
        required: 'Please enter your first name',
      },
      'files[]': {
        required: 'Please insert your 5 images',
      },
      }
});
3
  • what validator are you using? Commented Dec 14, 2015 at 7:54
  • I am using jquery validate library Commented Dec 14, 2015 at 7:55
  • It is working for all other validations. All the 40 validations that I have written in same manner; they work and this one doesnt show the custom error message. Commented Dec 14, 2015 at 7:58

3 Answers 3

1

Try the required attribute

<input class="form-control" type="file" name="files[]" id="files" accept="png|jpg|jpeg" required="required" multiple>

or:

rules:{
    'files[]': {
            required: true,
          },
},
message:{
    'files[]': {
            required: 'Please insert your 5 images',
          },
}

Note: you will need to create a new rule to count the 5 images

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

Comments

0

Replace code with this:

files[]: {
        required: true
      },

files[]: {
        required: 'Please insert your 5 images'
      },

Also, add

<input class="form-control" type="file" name="files[]" id="files" accept="png|jpg|jpeg" required="required" multiple>

2 Comments

I tried this code. Same result. It prints its custom message.. ie. This field is required
I have written same thing. Still it shows same result( This field is required).
0

You can also add simple function as following:
message: { required: "This field is required." }

Comments

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.