Critical Developers

Programmers Knowledge Base

Validate File Upload using REGEX / RegularExpressionValidator

Validate File Upload using RegularExpressionValidator

GUI
<asp:FileUpload id="FileUploadFH" runat="server"></asp:FileUpload>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUploadFH"
                    Display="Dynamic" ErrorMessage="Only .pdf,.doc,.docx,.xls,.xlsx,.rtf are allowed" ValidationExpression="(.*?)\.(Pdf|doc|docx|xls|xlsx|rtf)$"></asp:RegularExpressionValidator>

Code Behind
 if (FileUploadFH.HasFile)
   {
    FileUploadFH.SaveAs(Server.MapPath("../pdf/French/abc.doc") );
   }

Comments are closed