Custom Validator For File Upload Control

<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(source, args)
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!!!”);
args.IsValid = 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.”);
args.IsValid = false;

Call This Javascript Funcation For Custom  Validator Like

<asp:CustomValidator ID=”CustomValidator1″ runat=”server”  ClientValidationFunction=”CheckForTestFile”
ErrorMessage=”Please Upload Image”></asp:CustomValidator>


One thought on “ Custom Validator For File Upload Control

  1. I truly appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You’ve
    made my day! Thx again!

Leave a Reply

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

You are commenting using your 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