restrict the form to submit when attached documents are not in these formats( .pdf, .docx)

Ramesh_143
Giga Guru

I have a catalog item, when the user attaches any document while filling catalog form do not allow the form to submit, if the attached document are in these formats pdf, docx. 

 

Anyone help me how to fetch attached document names, format and to achieve the above thing.  I want to Perform this in Portal Side. If user attaches other than these documents show some error or alert message.

2 ACCEPTED SOLUTIONS

Aditya02
Tera Guru

hi @Ramesh_143 

 

you can achieve your functionality by writing an onSubmit() script for the form. Here is the script:

 

function onSubmit() {

    var a = this.document.getElementsByClassName('get-attachment');

    var b = a.length;

    //alert(b);

    if (!b) {

        g_form.addErrorMessage('You must add an attachment');

        return false;

    } else {

        for (var i = 0; i < a.length; i++) {

            var t = a[i].title;

            var fileExtension = t.split('.').pop().toLowerCase();

            if (fileExtension != 'docx' && fileExtension != 'pdf') {

                g_form.addErrorMessage(fileExtension + ' ' + 'You must add file types of docx and xlsx only');

                return false; // Prevent form submission

            }

        }

        return true;

    }

}

 

 

  • Here When I attached a document it will check weather it is in xlsx or docs or not. For Example, I am attaching Text document then it will give an error message.
  • The Below Client side Script works when the form is viewed on the ServiceNow Portal. It involves Manipulating the DOM (Document Object Model) Events.
  • Direct DOM access is not Possible on the ServiceNow Platform(Backend) using client-side scripts that run in users browser.

If my solution gets resolved your problem, Please give me a like and make my answer as 'correct answer'.

 

Thanks ,

Aditya

View solution in original post

VishaalRanS
Tera Guru

Hi @Ramesh_143 

 

To restrict document attachments in a ServiceNow catalog item to only specific formats (PDF and DOCX) on the portal side, you can use a client script to validate the file types before submission.

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

5 REPLIES 5

Slava Savitsky
Giga Sage

You need an onSubmit client script that would make an Ajax call to a client-callable script include, which in its turn would query the attachments, analyze their names, and return the result of the check to the client script.

To restrict form submission in ServiceNow when attached documents are not in PDF or DOCX format:

  1. Attach Client Script: Add a client script to the form's client scripts section.

  2. Validation Logic: Use JavaScript to check attachments before submission.

     
    function onSubmit() {
    var attachmentField = g_form.getControl('attachment_field_name'); if (attachmentField) { var attachments = attachmentField.files; for (var i = 0; i < attachments.length; i++) { var fileName = attachments[i].name.toLowerCase(); if (!fileName.endsWith('.pdf') && !fileName.endsWith('.docx')) { alert('Attach only PDF or DOCX files.'); return false; } } } return true; } g_form.submit = onSubmit;
  3. Implementation: Replace 'attachment_field_name' with your actual attachment field name.

  4. Testing: Ensure attachments are validated correctly during testing.

This approach uses client-side JavaScript to enforce file format validation before allowing form submission in ServiceNow.

Aditya02
Tera Guru

hi @Ramesh_143 

 

you can achieve your functionality by writing an onSubmit() script for the form. Here is the script:

 

function onSubmit() {

    var a = this.document.getElementsByClassName('get-attachment');

    var b = a.length;

    //alert(b);

    if (!b) {

        g_form.addErrorMessage('You must add an attachment');

        return false;

    } else {

        for (var i = 0; i < a.length; i++) {

            var t = a[i].title;

            var fileExtension = t.split('.').pop().toLowerCase();

            if (fileExtension != 'docx' && fileExtension != 'pdf') {

                g_form.addErrorMessage(fileExtension + ' ' + 'You must add file types of docx and xlsx only');

                return false; // Prevent form submission

            }

        }

        return true;

    }

}

 

 

  • Here When I attached a document it will check weather it is in xlsx or docs or not. For Example, I am attaching Text document then it will give an error message.
  • The Below Client side Script works when the form is viewed on the ServiceNow Portal. It involves Manipulating the DOM (Document Object Model) Events.
  • Direct DOM access is not Possible on the ServiceNow Platform(Backend) using client-side scripts that run in users browser.

If my solution gets resolved your problem, Please give me a like and make my answer as 'correct answer'.

 

Thanks ,

Aditya

VishaalRanS
Tera Guru

Hi @Ramesh_143 

 

To restrict document attachments in a ServiceNow catalog item to only specific formats (PDF and DOCX) on the portal side, you can use a client script to validate the file types before submission.

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.