Prevent Catalog item Submission if there is no attachment

 

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

  1. Retrieve Elements by Class Name:
    var attachmentElements = this.document.getElementsByClassName('get-attachment');
    • The this.document.getElementsByClassName method retrieves all HTML elements with the class name get-attachment.
    • The result is a live HTMLCollection, which is similar to an array and contains all matching elements on the form.
  2. Check for Attachments:
    if (attachmentElements.length === 0) {
    • The length property checks how many elements were found with the class name get-attachment.
    • If the length is 0, it means no attachments are present.
  3. Alert the User:
    alert("You must attach a Template Document.");
    return false;
    • If no attachments exist, an alert message is displayed to inform the user that they must attach a document.
    • return false prevents the form from being submitted.
  4. Allow Submission:
    return true;
    • If attachments exist, the function allows the form to be submitted by returning true.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here