Follow by Email

Wednesday, May 15, 2013

Validate Date using RegularExpressionValidator in Asp.net


Validate Date format using RegularExpressionValidator in Asp.net

1. Here i will show how to validate date format entered in a textbox using RegularExpressionValidator in asp.net.

Below is the syntax for a regular expression validator. In errorMessage property of the control we will write the message we want to show if the date format is wrong. In controlToValidate property we enter the id of the textbox which we want to validate. The ValidationExpression property has the expression which validates the values contained in the textbox.

Syntax:

<asp:RegularExpressionValidator ID="idName"
        runat="server"
        ErrorMessage="string"
        ValidationExpression="string">
        ControlToValidate="TextBoxId"
</asp:RegularExpressionValidator>

Example:
   
        <asp:TextBox ID="txtDateValid" runat="server" SkinID="Textboxdate"></asp:TextBox>

        <asp:RegularExpressionValidator ID="Reg1" runat="server" ControlToValidate="txtDateValid"
            ErrorMessage="Please enter date in dd/mm/yyyy format." ValidationExpression="([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([1-9]|0[1-9]|1[012])[- /.][0-9]{4}$" />
   

Implementation:

1

2