jQuery File Uploader JavaScript API: File Validation
Validators allow restricting user choice to certain kinds of files. For example,
you may want to only accept files less than 2MB in size or just PDF documents.
With our API, a validator is a function that receives a fileInfo
object for
every uploaded file and throws an exception if that file does not meet your set
requirements.
Validators can be called once the info on file becomes available. All unknown
fields used for validation have null
values when a validator is called. This
means a single validator can be called several times for a single file, each
time testing if the required field is not null
.
Validation inside jQuery File Uploader
Validators can also be added to the uploading widget through its validators
property.
This works for both single-file and multi-file widgets.
widget.validators
is a regular javascript array. One uploading widget can be assigned
several validators, and one validator can be assigned several widgets.
Note, you can’t rewrite validators with another object, you need to remove them from an original array.
Validation in JS API
You can add a validators
array to a Settings object of any API function where
it makes sense:
The list of such functions includes but is not limited to: openDialog
,
openPanel
, fileFrom
, filesFrom
, dragdrop.uploadDrop
, dragdrop.receiveDrop
.
It is better to use validators instead of checking file properties in
widget.onChange
and widget.onUploadComplete
callbacks. Validators are
activated before a dialog gets closed thus improving your UX.
Error messages
Each argument in the Error
constructor is a key in your locale file. Such keys
are mapped to messages shown to your users. The keys and messages can either be
predefined like size
, image
, or
custom.