Please disable your adblock and script blockers to view this page
 

RegularExpressionValidator to validate website URL, date format, numbers and decimal in asp.net textbox

Introduction: In this article I am going to share the following:
    Validate using RegularExpressionValidator in Asp.Net
  • How to check whether website address valid or not in asp.net
  • How to allow only numbers i.e. numeric values or digits in textbox
  • How to allow both number and decimal values in textbox
  • How to validate date format e.g. dd/mm/yyyy in textbox

In previous articles i explained How to Validate textbox, dropdownlist, checkboxlist, radiobuttonlist, listbox, checkbox and radiobutton controls and Jquery to validate email address using regular expression in asp.net

Implementation: Let’s create a demo page to demonstrate the validation.

HTML Source Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width:300px;">
                <legend>Validate using RegularExpressionValidator</legend>
                <table>
                    <tr>
                        <td>Website:</td>
                        <td>
                            <asp:TextBox ID="txtWebsite" runat="server" Style="width: 200px" placeholder="e.g. www.webcodeexpert.com"></asp:TextBox><br />
                            <asp:RegularExpressionValidator ID="rgetxtWebsite" runat="server"
                                ControlToValidate="txtWebsite" ErrorMessage="Not a valid website URL" Display="Dynamic" ForeColor="Red" SetFocusOnError="true"
                                ValidationExpression="^((ftp|http|https):\/\/)?([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+.*)$"></asp:RegularExpressionValidator></td>
                    </tr>
                    <tr>
                        <td>Pin Code:</td>
                        <td>
                            <asp:TextBox ID="txtPinCode" runat="server" Style="width: 200px"></asp:TextBox><br />
                            <asp:RegularExpressionValidator ID="rgePinCode" runat="server"
                                ControlToValidate="txtPinCode" SetFocusOnError="True" ForeColor="Red" Display="Dynamic" ErrorMessage="Enter numbers only"
                                ValidationExpression="^[0-9]\d*$"></asp:RegularExpressionValidator></td>
                    </tr>
                    <tr>
                        <td>Price:</td>
                        <td>
                            <asp:TextBox ID="txtPrice" runat="server" Style="width: 200px"></asp:TextBox><br />
                            <asp:RegularExpressionValidator ID="rgePrice" runat="server"
                                ControlToValidate="txtPrice" SetFocusOnError="True" ForeColor="Red" ErrorMessage="Only numeric or decimal numbers are allowed" Display="Dynamic"
                                ValidationExpression="^[0-9]\d*(\.\d+)?$"></asp:RegularExpressionValidator></td>
                    </tr>
                    <tr>
                        <td>Order Date:</td>
                        <td>
                            <asp:TextBox ID="txtOrderDate" placeholder="dd/mm/yyyy" runat="server" Style="width: 200px"></asp:TextBox><br />
                            <asp:RegularExpressionValidator ID="rgeOrderDate" runat="server"
                                ControlToValidate="txtOrderDate" ErrorMessage="Not a valid date format" ForeColor="Red" SetFocusOnError="true" Display="Dynamic"
                                ValidationExpression="^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"></asp:RegularExpressionValidator></td>
   </tr>
                </table>
            </fieldset>
        </div>
    </form>
</body>
</html>

Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates." 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon