Asp.Net File upload Validation From Javascript and Validator Control

Asp.Net File Upload Client Side Validation  and

<script language=”javascript” type=”text/javascript”>

function Trim(input)
{
var lre = /^\s*/;
var rre = /\s*$/;
input = input.replace(lre, “”);
input = input.replace(rre, “”);
return input;
}

// filter the files before Uploading for text file only
function CheckForTestFile()
{
var file = document.getElementById(‘<%=fuSelectPhoto.ClientID%>’);
var fileName=file.value;
//Checking for file browsed or not
if (Trim(fileName) ==” )
{
alert(“Please Select a File to Upload!!!”);
file.focus();
return false;
}

//Setting the extension array for diff. type of text files
var extArray = new Array(“.jpg”, “.jpeg”, “.gif”);

//getting the file name
while (fileName.indexOf(“\\”) != -1)
fileName = fileName.slice(fileName.indexOf(“\\”) + 1);

//Getting the file extension
var ext = fileName.slice(fileName.indexOf(“.”)).toLowerCase();

//matching extension with our given extensions.
for (var i = 0; i < extArray.length; i++)
{
if (extArray[i] == ext)
{
return true;
}
}
alert(“Please Only Upload Image File:  ”
+ (extArray.join(”  “)) + “\nPlease Select a New ”
+ “File to Upload and Submit again.”);
file.focus();
return false;
}

</script>

Call This Javascript function On Button OnClientClick Event

OnClientClick =”return CheckForTestFile();”

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