Click here to Skip to main content
15,888,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi Currently I've made a check if file exist or not in JavaScript,can anyone tell me how to check for certain extensions in the same function.
JavaScript
function validateUploadW9(source, e) {
    var fileName = FSObject.$("[id$=fupUploadW9]")[0].value;
    if (fileName == "") {
        var length = FSObject.$("[id$=lblUploadW9FileName]").length;
        if (FSObject.$("[id$=lblUploadW9FileName]")[length - 1].innerHTML == "") {
            e.IsValid = false;
        }
        else {
            e.IsValid = true;
        }
    }
    else {
        e.IsValid = true;
    }
}

Please post your answers ASAP.
Posted

1 solution

C#
var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
    if (fileName == "")
    {
       var length = FSObject.$("[id$=lblUploadW9FileName]").length;
       if (FSObject.$("[id$=lblUploadW9FileName]")[length - 1].innerHTML == "")
        {
           e.IsValid = false;
       }
       else
       {
           if (fileExtension != ".doc" || ".DOC " || ".txt" || ".docx" || ".DOCX" || ".TXT" || ".xls" || ".xlsx" || ".XLS" || ".XLSX" || ".pdf" || ".PDF")
            {
                FSObject.$("[id$=cusUploadW9]")[0].innerHTML = "Invalid extension, only .doc, .docx, .txt, .xls, .xlsx, .pdf and .jpg extensions are supported";
                e.IsValid = false;
            }
            else
           {
               e.IsValid = true;
           }
       }
   }
   else {
       if (fileExtension != ".doc" || ".DOC " || ".txt" || ".docx" || ".DOCX" || ".TXT" || ".xls" || ".xlsx" || ".XLS" || ".XLSX" || ".pdf" || ".PDF")
       {
           FSObject.$("[id$=cusUploadW9]")[0].innerHTML = "Invalid extension, only .doc, .docx, .txt, .xls, .xlsx, .pdf and .jpg extensions are supported";
           e.IsValid = false;
       }
       else
       {
           e.IsValid = true;
       }
     }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900