locked
Regular Expression for CSV file ...??? RRS feed

  • Question

  • User1208695299 posted

    Hiii frndsssss...
     Here m doing file uploading....
    i want to upload only .CSV file, so i need regular expression... please... :)

    Thank You

    Friday, June 20, 2014 12:17 AM

Answers

  • User1208695299 posted

    This expression is working for me....

    "^.*\.(csv)$"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 20, 2014 6:53 AM

All replies

  • User2103319870 posted

    You can use the below regular expression to validate the file extension.

    ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.csv|.CSV)$

    Complete Code:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
            function validate() {
                var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID%>').value;
                //Regular Expression for fileupload control.
                var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.csv|.CSV)$/;
                if (uploadcontrol.length > 0) {
                    //Checks with the control value.
                    if (reg.test(uploadcontrol)) {
                        return true;
                    }
                    else {
                        //If the condition not satisfied shows error message.
                        alert("Only .csv files are allowed!");
                        return false;
                    }
                }
            } 
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validate();" />
        </div>
        </form>
    </body>
    </html>

    Source URL

    Friday, June 20, 2014 1:26 AM
  • User1208695299 posted

    This expression is working for me....

    "^.*\.(csv)$"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 20, 2014 6:53 AM