Field validation for file upload control element
Here's a field validation for the File Upload Control element. I've only tried this where there is one Upload Control.
Here's a field validation for the File Upload Control element. I've only tried this where there is one Upload Control.
Copy the code below into the JSHeader event. I call the code from an action button's onClick event.
The code only allows for 1 file to be attached to a document. If a file already exists on the document, and another file is selected from the browse button, then the existing document is marked for deletion. This existing file is replaced with the newly selected file with no needed intervention from the user. Also the document can not be submitted until a file has been selected, or some smart user puts a '\' in the upload field.
function BOCRfile () { var doc=document.forms[0]; var pattern = /%%Detach.(d{1,4})/i; var filepattern = //; attachments = document.images.length; goodFilename = ""; for ( var i=0; i < doc.length; i++ ) { if ( doc.elements[i].type=="file" ) { ULname = doc.elements[i].name; FPath=doc.elements[i].value; elemNum = i; } else if ( pattern.test ( doc.elements[i].name ) ) { // alert ( 'element name is: ' + doc.elements[i].name + ' - type is: ' + doc.elements[i].type + ' - value is: ' + doc.elements[i].value + ' - element #: ' + i ); FileElemNum = i; doc.elements[FileElemNum].checked = true; } } if ( filepattern.test ( doc.elements[elemNum].value ) ) { goodFilename = "Y"; } if ( ( attachments < 4 ) && ( FPath=="" ) ) { msg1 = "You must attach the BackOrder Commit file to this document before clicking the Save and Close button. "; msg2 = "If you don't want to save this document, click the 'Back' button."; alert ( msg1 + msg2 ); doc.elements[elemNum].focus (); return false; } else if ( goodFilename=="" ) { alert ( 'You have entered an invalid filename in the 'Browse' button field. Please select the file again.' ); doc.elements[elemNum].focus (); return false; } else { if ( FPath=="" && doc.elements[FileElemNum].checked ) { doc.elements[FileElemNum].checked = false; } doc.submit () } }