elclanrs / jq-idealforms-old

The ultimate framework for building and validating responsive HTML5 forms.
665 stars 95 forks source link

File Upload Size #50

Closed 4michaelcoleman closed 12 years ago

4michaelcoleman commented 12 years ago

Does it support checking the size or dimensions of media (i.e. 400px x 400px, or 20kb)?

elclanrs commented 12 years ago

Nop, it doesn't because the file API is not supported in IE9-8, and I don't want to pollute the code with polyfills, that's the main reason, but I might add support in the future... But you can add your own filter if you don't need to support IE. Check the file API docs to learn more. If you want to check for dimensions check this question at StackOverflow To check for filesize you can do something like this, adjust to your needs:

var filesize = {
  regex: function(input, value) {
    var usersize = input.userOptions.data.filesize;
    // I'm assuming there's just one file 
    // but you can always loop the files array
    // and get the size for each file
    var size = input.input[0].files[0].size;
    this.error = 'Filesize must be less than '+ usersize;
    return size <= usersize;
  }
};

$.extend($.idealforms.filters, filesize);

// Usage
var $myform = $('#myform').idealforms({
  inputs: {
    'myfile': {
      filters: 'filesize',
      data: { filesize: 1024 }
    }
  }
});

I also found this code that was meant for jquery validator but I'm sure it can be easily ported to Ideal Forms.

I you want crossbroswer support you can do some AJAX magic with PHP to get the data from the server and then analyze it with Ideal Forms in the front-end.