I am working on asp.net web application in which i have 6 FileUpload Controls. for one file upload i have created a javascript method to check file extension and file size. but how to pass id of FileUpload upload as a parameter so that only in only one method i can validate all FileUpload my code of javacript is
var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "htm", "html", "rar", "zip", "txt", "pdf"];
function CheckExtension() {
var file = document.getElementById("<%=txtTenderDoc.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) {
alert("Invalid File. Unknown Extension Of Tender Doc" + "Valid extensions are:\n\n" + validFilesTypes.join(", "));
}
return isValidFile;
}
function validateFileSize() {
var file = document.getElementById("<%=txtTenderDoc.ClientID%>");
var fileSize = file.files[0].size;
var isValidFile = false;
if (fileSize !== 0 && fileSize <= 25214400) {
isValidFile = true;
}
if (!isValidFile) {
alert("File Size Should be Greater than 0 and less than 25 mb");
}
return isValidFile;
}
and i have used this in aspx page
<asp:FileUpload ID="txtTenderDoc" onchange="var result= CheckExtension();validateFileSize(); return result"
runat="server"></asp:FileUpload>
as you can see i have to create
6 method by this to check file size and file extension how to do that in only one method....
Updated
after seeing
this
i have changed my javascript to
var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "htm", "html", "rar", "zip", "txt", "pdf"];
function CheckExtension(Id)
{
var file = document.getElementById(Id);
debugger;
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) {
alert("Invalid File. Unknown Extension Of Tender Doc" + "Valid extensions are:\n\n" + validFilesTypes.join(", "));
}
return isValidFile;
}
function validateFileSize(Id) {
var file = document.getElementById(Id);
var fileSize = file.files[0].size;
var isValidFile = false;
if (fileSize !== 0 && fileSize <= 25214400) {
isValidFile = true;
}
if (!isValidFile) {
alert("File Size Should be Greater than 0 and less than 25 mb");
}
return isValidFile;
}
and calling it like this
<asp:FileUpload ID="flTenderDoc" onchange="var result=CheckExtension('flTenderDoc');validateFileSize('flTenderDoc'); return result"
runat="server"></asp:FileUpload>
but it is not working ......
can anyone help this is my first javascript method :)