Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am using FileUpload Control to upload Files. In which I have 2 methods in javascript first to check file extension and second for checking file size how to call both methods on Event
onchange



my javascript Code is
JavaScript
var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "htm", "html", "rar", "zip", "txt", "pdf"];
       function ValidateFile() {
           var file = document.getElementById("<%=txtTenderDoc.ClientID%>");
           var label = document.getElementById("<%=lblTenderDocExceptionMessage.ClientID%>");
           var path = file.value;
           var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
           var isValidFile = false;
           for (var i = 0; i < validFilesTypes.length; i++) {
               if (ext == validFilesTypes[i]) {
                   isValidFile = true;
                   break;
               }
           }
           if (!isValidFile) {
               label.style.color = "red";
               label.innerHTML = "Invalid File. Unknown Extension Of Tender Doc" + "Valid extensions are:\n\n" + validFilesTypes.join(", ");
               label.focus();
           }
           return isValidFile;
       }

       function validateFileSize() {
           debugger;
           var file = document.getElementById("<%=txtTenderDoc.ClientID%>");
           var label = document.getElementById("<%=lblTenderDocExceptionMessage.ClientID%>");
           var fileSize = file.files[0].size;

           if (fileSize == 0) {
               label.style.color = "red";
               label.innerHTML = "File Size Should be Greater than 0";
               return false;
           }
           else
               return true;
       }

and on aspx page i have called like this
ASP.NET
<asp:FileUpload ID="txtTenderDoc" onchange=" return ValidateFile(); return validateFileSize();" runat="server"></asp:FileUpload>

But the problem in only 1st method is called using this
can anyone Help
Thanks
Vishal Pandey
Posted
Updated 17-Dec-13 0:16am
v3

1 solution

Its since return after the first method so the subsequent
method wont be called.

Assign to a variable and return it later like below

onchange="var result= ValidateFile();validateFileSize(); return result"


Hope this helps...
 
Share this answer
 
Comments
Vishal Pand3y 17-Dec-13 6:18am    
thanks JoCodes it's working
JoCodes 17-Dec-13 6:36am    
You are welcome...:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900