Validation of file names in JavaScript

Last post 06-17-2009, 11:39 PM by cutechat. 2 replies.
Sort Posts: Previous Next
  •  06-17-2009, 12:14 AM 53195

    Validation of file names in JavaScript

    Hi,
     
    I need to check the selected files *before* upload to make sure they have the correct file names.
     
    How can I do this in JavaScript?
     
    Thanks in advance. 
  •  06-17-2009, 10:41 PM 53237 in reply to 53195

    Re: Validation of file names in JavaScript

    Bump.
     
    Can anyone assist? 
  •  06-17-2009, 11:39 PM 53238 in reply to 53237

    Re: Validation of file names in JavaScript

    Hi,
     
    Please check this sample :
     
    1. <%@ Page Language="C#" Title="First sample" %>  
    2. <%@ Import Namespace="CuteWebUI" %>  
    3. <%@ Register TagPrefix="CuteWebUI" Namespace="CuteWebUI" Assembly="CuteWebUI.AjaxUploader" %>  
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">  
    5.   
    6. <script runat="server">  
    7.   
    8.     string disabledExtList = "aspx,asp,ashx,html,htm,mht,exe,dll,php,jsp";   
    9.   
    10.     void InsertMsg(string msg)   
    11.     {   
    12.         ListBoxEvents.Items.Insert(0, msg);   
    13.         ListBoxEvents.SelectedIndex = 0;   
    14.     }   
    15.     protected void UploadAttachments1_AttachmentAdded(object sender, AttachmentItemEventArgs args)   
    16.     {   
    17.         InsertMsg("Added.." + args.Item.FileName);   
    18.     }   
    19.   
    20.     protected void UploadAttachments1_FileValidating(object sender, UploaderEventArgs args)   
    21.     {   
    22.         //validate the extensions , this is very important!   
    23.         //the client side validation is not safe , double check it here:   
    24.         string ext=Path.GetExtension(args.FileName).TrimStart('.').ToLower();   
    25.         ext = "," + ext + ",";   
    26.         string list="," + disabledExtList.ToLower() + ",";   
    27.         if (list.IndexOf(ext) != -1)   
    28.         {   
    29.             throw (new Exception("Invalid file type!"));   
    30.         }   
    31.     }   
    32. </script>  
    33.   
    34. <html xmlns="http://www.w3.org/1999/xhtml">  
    35. <head id="Head1" runat="server">  
    36. </head>  
    37. <body>  
    38.     <form id="Form1" runat="server">  
    39.         <CuteWebUI:UploadAttachments runat="server" ID="UploadAttachments1" OnAttachmentAdded="UploadAttachments1_AttachmentAdded" OnFileValidating="UploadAttachments1_FileValidating">  
    40.         </CuteWebUI:UploadAttachments>  
    41.         <br />  
    42.         <div>  
    43.             Server Trace:   
    44.             <br />  
    45.             <asp:ListBox runat="server" ID="ListBoxEvents" Width="800"></asp:ListBox>  
    46.         </div>  
    47.     </form>  
    48.     <script type="text/javascript">  
    49.     var disabledExtList='<%=disabledExtList %>'  
    50.     </script>  
    51.     <script type="text/javascript">  
    52.     //validate the extensions in client side   
    53.     //this way is not safe , just for performance   
    54.     //try to disable it to test the server validation   
    55.     var useclientvalidation=true;   
    56.     function CuteWebUI_AjaxUploader_OnSelect(files)   
    57.     {   
    58.         if(useclientvalidation)   
    59.         {   
    60.             var list=","+disabledExtList+",";   
    61.             for(var i=0;i<files.length;i++)   
    62.             {   
    63.                 var fps=files[i].FileName.split('.');   
    64.                 var ext=fps[fps.length-1].toLowerCase();   
    65.                 ext=","+ext+",";   
    66.                 if(list.indexOf(ext)!=-1)   
    67.                 {   
    68.                     alert("Javascript : Invalid file type : "+ext);   
    69.                     //cancel it.   
    70.                     return false;   
    71.                 }   
    72.             }   
    73.         }   
    74.     }   
    75.     </script>  
    76.   
    77. </body>  
    78. </html>  
     
    Regards,
    Terry
View as RSS news feed in XML