1

I want to validate the file extension before upload so,I create the fileUpload control as following

<asp:FileUpload ID="FileUpload1" runat="server" OnChange="return validateFileExtension(this)"/> 

and create java script to validate extension of file before upload

<script lang="javascript" type="text/javascript">
var validFileExtensions = [".txt", ".TXT"];
function ValidateFileUpload(Source, args) {
var fuData = document.getElementById('<%= FileUpload1.ClientID%>');
var FileUploadPath = fuData.value;
if (FileUploadPath == '') {
// There is no file selected 
args.IsValid = false;
}
else {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "txt" || Extension == "TXT") {
args.IsValid = true; // Valid file type
FileUploadPath == '';
 }
 else {
 alert("Please upload only text file")
 }
 }
 }
</script> 

and I got 0x800a1391 - Javascript runtime error: 'validateFileExtension' is undefined

1
  • change validateFileExtension() to ValidateFileUpload() as OnChange="return validateFileExtension(this) accepts a function and validateFileExtension() is not a function Commented Jun 10, 2015 at 5:20

1 Answer 1

0
<asp:FileUpload ID="FileUpload1" runat="server" OnChange="return ValidateFileUpload(this)"/>

ValidateFileUpload instead of validateFileExtension

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.