DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

File Extension Validation In Asp.net

01.27.2012
| 1987 views |
  • submit to reddit
        // file extension validation in asp.net
function ValidateFileUploaded() {
                    var frm = document.forms[0];
                    var fileExtension;
                    var isValid = true;
                    var flag = false;
                    var cnt = 0;
                    var allowedFileType;
                    allowedFileType= '.xls';
 
                    for (i = 0; i < frm.elements.length; i++) {
 
                        if (frm.elements[i].type == "file") {
                            extension = frm.elements[i].value.substring(frm.elements[i].value.lastIndexOf('.')).toLowerCase();
                            if (frm.elements[i].value != "") {
                                if (fileExtension != allowedFileType) {
                                    isValid = false;
                                    break;
                                }
                            }
                        }
                    }
 
                    if (isValid) {
                        return true;
                    }
                    else {
                        alert("Only " + allowedFileType+ " files can be uploaded.");
                        return false;
                    }