Input validation for file attachments using JavaScript
A solution is to validate the value of the HTML input element instead.
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 () { for(i=0;i<document.forms[0].elements.length;++i) { 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 == ""){
—Loks
alert ("please attach a file");
return false;
}