Sometimes, you need to make sure users attach a file before submitting a catalog item—maybe it’s a form, a screenshot, or some kind of supporting document. To avoid incomplete requests, you can set up a simple rule that blocks the submission if no attachment is added.
function onSubmit() {
// Retrieve elements with the 'get-attachment' class using the document object
var attachmentElements = this.document.getElementsByClassName('get-attachment');
// Check if there are any attachment elements
if (attachmentElements.length === 0) {
// If no attachments are found, alert the user with a message and prevent submission
alert("You must attach at lease one Document.");
return false; // Block form submission
}
// If attachments exist, allow the form to be submitted
return true;
}
Explanation
- Retrieve Elements by Class Name:
- The
this.document.getElementsByClassNamemethod retrieves all HTML elements with the class nameget-attachment. - The result is a live HTMLCollection, which is similar to an array and contains all matching elements on the form.
- The
- Check for Attachments:
- The
lengthproperty checks how many elements were found with the class nameget-attachment. - If the length is
0, it means no attachments are present.
- The
- Alert the User:
- If no attachments exist, an alert message is displayed to inform the user that they must attach a document.
return falseprevents the form from being submitted.
- Allow Submission:
- If attachments exist, the function allows the form to be submitted by returning
true.
- If attachments exist, the function allows the form to be submitted by returning








