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?