Input validation for file attachments using JavaScript

You Can View User Feedback To This Tip

We all have learned that for performance reasons, we should perform field input validations using JavaScript if the user fills a form using a web client.

To validate the attachment of a file, however, it is a bit more difficult since the file isn't uploaded until the form is submitted. A solution is to validate the value of the HTML input element instead. The standard Notes file upload control generates an HTML input element (type = "file") which takes the absolute path of the file to be uploaded. The following code validates the contents of that element. It can be put into the onSubmit event of the form along with the rest of the field validation functions.

function validateFileUpload () {
  var elem = document.forms[0].elements[i];
  if (elem.type == "file") {
    alert ("Element-Value: " + elem.value);
    if (elem.value == "") {
        alert ("please attach you file");
        return ("");


  • One of our friends, Lothar Mueller, submitted a code to validate the file upload control. What he checked was for an entry in that control. However, the code he wrote was not efficient. That way you would need to traverse through all the elements on the form. Here is a much simpler way. Go to File Upload Control properties by right clicking on it. Go to the HTML tab and specify an ID such as fileupload. Then use this ID in the validation of the onSubmit event.

    if (document.forms[0].fileupload.value == ""){
    alert ("please attach a file");
    return false;


This was first published in July 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.