0

I've added my own "maxfilesize" jquery.validate method, and am using the unobtrusive validation script from Microsoft. The following is working, but I'm wondering if there a better way to do this with unobtrusive jquery validation?

Here's the signature of my validator add-on. It sums the size of every file input element in the form it is within, and errors if the total upload size is greater than maxMB.

$.validator.addMethod('maxfilesize', function (value, element, maxMB) { ... });

I place this just before the opening <form> element. It's just a dummy input element that I'm using to trigger form validation.

<form>
  <input maxfilesize="4" style="visibility: hidden" aria-hidden="true" />

  <input type="file" name="Files" />
  <input type="file" name="Files" />
  <input type="file" name="Files" />

  <button type="submit">Upload Files</button>
</form>

I wish I could place the maxfilesize attribute directly on the form element, but unobtrusive jquery validation doesn't pay any attention to it. Any suggestions?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Jeremy Cook
  • 20,840
  • 9
  • 71
  • 77

1 Answers1

0

Why are you not checking HTML5 file size? A post is available here

<input type="file" name="Files" id="myFile1"/>
<input type="file" name="Files" id="myFile2"/>
<input type="file" name="Files" id="myFile3"/>

var myFile1Size = document.getElementById('myFile1').files[0].size;
// check file size
var myFile2Size = document.getElementById('myFile2').files[0].size;
// check file size
var myFile3Size = document.getElementById('myFile3').files[0].size;
// check file size
Community
  • 1
  • 1
Sudipta Kumar Maiti
  • 1,669
  • 1
  • 12
  • 18
  • The method I've described already works (using the files[0].size property in fact). I'm wondering if there is a better way to do this that is compatible with jquery validator. – Jeremy Cook Dec 11 '15 at 19:10