Pages

Monday, September 16, 2013

REGEX for Time format (hh:mm)

REGEX for "hh:mm" is:

^(1[0-2]|0[1-9]):([0-5][0-9])$

Using this Regex pattern, user can enter the Hours between 01-12 & Minutes between 00-59,  with the Separator ':'

Use this code :

"txt_time" textbox takes time as input.

 <asp:TextBox ID="txt_time" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Display="None"
  ErrorMessage="Enter Valid Time(HH:mm)" ValidationExpression="^(1[0-2]|0[1-9]):([0-5][0-9])$"
  ValidationGroup="headlink" ControlToValidate="txt_time">

</asp:RegularExpressionValidator>






<asp:ValidationSummary ID="ValidationSummary2" CssClass="erroesummary" runat="server" ValidationGroup="headlink" HeaderText="The Following Fields Are Mandatory :" ShowMessageBox="false" DisplayMode="BulletList" ShowSummary="true" />


<asp:Button ID="btn" runat="server" OnClick="btn_Click"
                                    ValidationGroup="headlink" />



Results:


12:59        Correct
11:61        Incorrect
01:01        Correct
01*01       Incorrect
09:88        Incorrect

 

No comments:

Post a Comment