开发者

How to validate user input file extensions

I have a form where administrator can define file extensions that are allowed. 开发者_JS百科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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜