ASP.Net File Upload file extension Validation Client Side

To Validate file extension of File before upload use below  Jquery code.

< script type = “text/javascript” >

function Validation() {

var uploadcontrol = $(“[id$=fuImageUpload]”).val();

if (uploadcontrol.length > 0) {

var validFilesTypes = [“bmp”, “gif”, “png”, “jpg”, “jpeg”, “doc”, “xls”];

var file = $(“[id$=fuImageUpload]”).val();
var path = file;
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. Please upload a File with extension:\n\n” + validFilesTypes.join(“, “));
return false;
}
} else {
alert(“Please select file.”);
return false;
}

}

< / script >

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s