Using Constraints for Attachments

You may wish to enforce that only a certain type or size of file is uploaded into an attachment field. This can be done by using the Validation Constraints.

  1. When you click on your attachment field, there will be a (probably closed) Validation section on the right. Click on this.
  2. Once you click on Validation to expand this section, you will see Custom Constraint. Set this to Enabled.
  3. Once you have Enabled the Custom Constraint, you will see the Expression and Message fields. This is where you will enter the necessary details to restrict your attachment(s).

Say you are expecting a specific spreadsheet be uploaded. To allow only XLS attachments enter the following in the Expression field:

value.filter(function(item) { return !\.xls$/i) }).length === 0

Enter your own custom Message into the Message field such as:

Please upload only .xls spreadsheets into this field.

The expression works like this: filter the values of this field. If any of the item names don’t end in xls (case insensitive), fail the condition.

NOTE: the javascript function match uses regular expressions, so if you wanted to check for .xls OR .xlsx files it would be

value.filter(function(item) { return !\.xls(x)*$/i) }).length === 0

If you want to allow for .xls, .xlsx, .doc, or .docx it would be:

value.filter(function(item) { return !\.(xls(x)*|doc(x)*)$/i) }).length === 0

Here is a tool for testing your regular expressions.

Here is a similar constraint to not allow files over 5 MB:

value.filter(function(item) { return item.size > 5242880 }).length === 0

Note that the file size is represented in bytes, but the logic is essentially the same.

So if you wanted to allow  .xls, .xlsx, .doc, or .docx files under 5 MB, the expression would just be:

value.filter(function(item) { return !\.(xls(x)*|doc(x)*)$/i) }).length === 0 && value.filter(function(item) { return item.size > 5242880 }).length === 0


Now, you may have noticed this wonderful feature that allows users to upload multiple attachments into one field.


This means that you don’t have to know in advance exactly how many attachments are appropriate or show lots of additional attachments fields when a few are needed but not necessarily all of them.

This feature does not affect the use of the above constraints at all. It does add a new possibility, though.

Perhaps you want to allow multiple files, but only up to 3. You can do this with constraints as well. For attachments, the answer is going to contain all of the attachments and you can check it’s size. So the constraint expression would be:

value.length < 4

To allow a maximum of 3 files.

Similarly, if you must have 2 logs uploaded, you can require at least 2 files:

value.length > 1

Putting a few of them together, you could require at least 2 files with extenstion .log:

value.length > 1 && value.filter(function(item) { return !\.log$/i) }).length === 0