Fraz Sundal
Fraz Sundal

Reputation: 10448

How to validate user input file extensions

I have a form where administrator can define file extensions that are allowed. I have given a textbox for file extensions where admin can write extensions, comma separated for multiple. Now i want to know how can i validate that user input contains all the valid extensions like doc,docx,jpg,mp3,wmv.

I am using JQuery and C# in an Asp.Net MVC application

Upvotes: 0

Views: 1184

Answers (1)

ehudokai
ehudokai

Reputation: 1926

bind an event to your file element change event and read the filename from it to verify that it is one of your allowed types.

example:

 $("#yourFileElementId").change(function(ev){
      var filename = $(this).val();
      if(/(\.doc$)|(\.docx$)|(\.jpg$)|(\.mp3$)|(\.wmv$)/.match(filename)){
          // filename has one of the extensions
      } else {
          alert("only specified file types are allowed");
          $(this).val(""); //This should empty the file input.
      }
 }

Hope that helps!

Upvotes: 1

Related Questions