.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Validate the extension of the file uploaded in the file upload control in asp.net

Posted By:Maverick       Posted Date: July 26, 2011    Points: 40    Category: Javascript    URL: http://www.dotnetspark.com  

Validate the extension of the file uploaded in the file upload control in asp.net. It will verify the extension specified in the code and the extension of the uploaded file matches or not.
 

Hello All,
The javascript below will validate the file to be uploaded in the file upload control. It will verify the extension specified in the code and the extension of the uploaded file matches or not.

function ValidateFileUploadForResult() {
                    var frm = document.forms[0];
                    var extension;
                    var isValid = true;
                    var flag = false;
                    var cnt = 0;
                    var currentFileType;
                    currentFileType = '.xml';
 
                    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 (extension != currentFileType) {
                                    isValid = false;
                                    break;
                                }
                            }
                        }
                    }
 
                    if (isValid) {
                        return true;
                    }
                    else {
                        alert("Only " + currentFileType + " files are allowed.");
                        return false;
                    }
                }
Specify the file type you want to validate in the "currentFileType" variable.

Hope it helps.

Thanks,
Maverick

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend