Checking for attachments in JavaScript

Have you ever needed to validate a form and check that there is at least one attachment? I did this recently and came up with the following solution.

Place the following Formula in to the HTML Header, $$HTMLHead field or in to a Computed Text hotspot.

"<script type=\"text/javascript\">"+@NewLine+
" var numOfAttachments = " + @Text(@Attachments) + ";"+@NewLine+

This will produce the following HTML in a document that has one attachment.

<script type="text/javascript">
var numOfAttachments = 1;

This number variable it global as it is defined outside of any functions. This means that it can be used in ANY of your JavaScript functions on that page. Consider the following function that is called before the form can be submitted:

function validate( frm ){
//only validate your upload if there are no attachments already...
if ( numOfAttachments == 0 ) {
if ( frm.nameOfUpload.value == '' ) {
alert('You need to upload at least one file');
return false;
return true;

If you don't know the name of your upload control (none R5 users!) then consider using this method to get a handle on it.

Note: This method is not perfect!! If you have an attachment on a document and the user has marked it for deletion using the check box at the bottom, the validation function will not check for a new upload and the document will then have NO attachments.

To get around this consider adding the following line to the function which checks to see if they have checked the text box. This takes account of the fact that the delete attachments check boxes will always be the last elements on the document:

if(numOfAttachments == 0 || (numOfAttachments ==1 && frm.elements[frm.elements.length-1].checked)){


    • avatar
    • Kim
    • Wed 13 Dec 2000

    How to clear the value of the FileUpload Control

    i'd like to clear the value of the Fileupload control when use click a button. my code is like this : for(var e=0 ; e<document.forms[0].elements.length ; e++) if( document.forms[0].elements[e].type=='file'){ alert(document.forms[0].elements[e].value); document.forms[0].elements[e].value=''; }

    'alert' is ok, but setting value doesn't work. the value is read-only?? do you have any idea?

      • avatar
      • Jake
      • Thu 14 Dec 2000

      You can't..

      There is a good reason as well. Security.

      Imagine if a script could set the file upload value to the path of any file on the user's hard-drive and then upload it to the server....


      • avatar
      • Lake
      • Thu 29 Sep 2005

      Re: How to clear the value of the FileUpload Control

      I am using this technique to wipe out the value and reset the file control back to its starting point in the DOM if I don't like the value the user is trying to upload: Call function from onChange event of file upload control.

      function alertUploadComplete( uploadObj ) { //this function first, will detect the file upload for double spaces. If there is one //alert user and remove it from the field and ask for new upload. var spacesStr = " "; var alertMsgStr = "Your file attachment contains more than 1 consecutive space.\n"; alertMsgStr += "Please rename the file without using more than 1 consecutive\n"; alertMsgStr += "space.\n\n"; alert( uploadObj.id) if ( uploadObj.value.indexOf(spacesStr) != -1) { alert(alertMsgStr);

      h = '<input id="' + uploadObj.id + '" style="font: 12px; color : #000000;" title="Select file to attach" onChange="alertUploadComplete(this)" type="file" name="' + uploadObj.name + '"></td><td width="192" valign="middle">' uploadObj.outerHTML = h; } else { alert("Click the Save button to complete saving the Attachment."); } }

      Show the rest of this thread

    1. I am unable to attached documents at any size

      error message show that "javascript:attachment(0)"

      kindly help us

      ketan prajapati

    • avatar
    • Jai
    • Fri 25 Feb 2005

    Problem with attachments


    I am doing a server side validation in the webquerysave event of the form. If any of the required fields are not filled, the attached file is not saved with the document. How do I ensure that the attached file is saved even if there are some validations on the form.

    If you have any idea ...pls help

    1. Re: Problem with attachments

      Then Do Like this

      <FORM> <INPUT TYPE="file" NAME="elementName" id="elementName"> <INPUT TYPE="button" onclick="Clear();" value="Clear"> </FORM> <Script Language="JavaScript"> function Clear() { document.getElementById("elementName").outerHTML = document.getElementById("elementName").outerHTML; } </Script>

      Show the rest of this thread

Your Comments


Navigate other articles in the category "JavaScript"

« Previous Article Next Article »
Making pop-up windows modal   Remind users to save their forms

About This Article

Author: Jake Howlett
Category: JavaScript
Keywords: attachment; upload; limit;


Print Friendly

Let's Get Social

About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »