Question

the jQuery validate has an extra plugin called accept

http://jqueryvalidation.org/accept-method

what this allows you to do is validate file types on an upload form for example

so you could have a bit of code that looks like

$("#addNewDocumentForm").validate({
rules: {
     inputDocument: { required: true, accept: "png|jpe?g|gif|pdf"  }
},
messages: {
     inputDocument: "File must be PDF, DOCX, JPG, GIF or PNG"               
},
submitHandler: function(e) {
      ...

the problem is using this in ie 8 or 9 because while it will do the required part it will not do the accept bit as it uses the file API part of HTML5

I was wondering if anyone has come across any similar issues? any if they found any polyfills for it etc?

Was it helpful?

Solution

You are making one of two possible errors:

  1. using the wrong rule for validating the file extension.
  2. using the wrong parameters for validating the mime-type.

The accept method is only for validating by mime-type.

Assuming you want to validate by file extension, then you'll need to use the extension method.

Read and compare the documentation for the two methods: extension and accept.

Both accept and extension are part of the plugin's own additional-methods.js file.

$("#addNewDocumentForm").validate({
    rules: {
        inputDocument: {
            required: true,
            extension: "png|jpg|gif|pdf"
        }
    },
    messages: {
        inputDocument: "File must be PNG, JPG, GIF or PDF"               
    },
    submitHandler: function(form) {
        ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top