Hello guys,
I have come up with a scenario where I want to validate a textbox with UTC format:
e.g: +05:30 -01:05 (this can be any time format)
I need to validate my textbox strictly, the user must be forced to enter 2 digits after + or minus sign and 2 digits after the colon.
Validation:
1) 1st character must be + or - only
2)next accept only 2 digits then append colon then 2 digits after colon.
Can anybody tell me how do i validate this utc format:
so far i have done this:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="home.aspx.cs" Inherits="reg.home" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src="scripts/jquery-3.3.1.js"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:TextBox ID="TextBox1" runat="server"
- onkeyup="sayKeyCode(event,this.value);">
- </asp:TextBox>
- </div>
- <asp:RegularExpressionValidator ID="rgx" ControlToValidate="TextBox1" runat="server"
- ErrorMessage="only 2 digit is allowed before and after colon" ForeColor="Red"
- Display="Dynamic"
- ValidationExpression="^[0-9]{0,9}(\:[0-9]{2,2})?$"></asp:RegularExpressionValidator>
- </form>
- <script type="text/javascript" language="javascript">
- function sayKeyCode(event, v)
- {
- var TextBox = document.getElementById('<%=TextBox1.ClientID%>');
-
- if(event.keyCode != 8)
- {
- if (TextBox.value.length == 2 && TextBox.value.length != 3)
- {
- TextBox.value = TextBox.value + ":";
- }
- else
- {
- TextBox.value = TextBox.value;
- }
- }
- }
- </script>
- </body>
- </html>