How to validate the file upload only for word file (doc) and PDF file using regular expression in asp.net C#

In this article,  I am validate the file upload to word file or pdf file ,I have used a file FileUpload to upload a pdf  or doc (word file). For the validation I am using the regular expression validator. We can validate this to just write the below code.
ValidationExpression="^.+(.doc|.docx|.DOC|.DOCX|.pdf|.PDF)$"

Design:-



Source code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileuploader.aspx.cs" Inherits="fileuploader" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            font-size: xx-large;
        }
        .style2
        {
            font-size: large;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <strong><span class="style2">Resume</span><span class="style1"> </span></strong>


        <asp:FileUpload ID="Fileuploaderresume" runat="server"></asp:FileUpload>
       
                      <asp:RegularExpressionValidator ID="REVresume" runat="server"
                          ControlToValidate="Fileuploaderresume" Display="Dynamic"
                          ErrorMessage="Only word file  or pdf  are allowed"
                          ValidationExpression="^.+(.doc|.docx|.DOC|.DOCX|.pdf|.PDF)$"
            Font-Bold="True" Font-Italic="True" ForeColor="#CC3300" SetFocusOnError="True"></asp:RegularExpressionValidator>
    </div>
    </form>
</body>
</html>

Out Put:-





How to validate the file upload only for word file (doc) and PDF file using regular expression in asp.net C# How to validate the file upload only for word file (doc) and PDF file using regular expression in asp.net C# Reviewed by NEERAJ SRIVASTAVA on 7:37:00 PM Rating: 5

No comments:

Powered by Blogger.