Shaksham Singh

Shaksham Singh

  • NA
  • 17
  • 5.7k

Utc Validaton using regular expression

Apr 2 2018 5:34 AM
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:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="home.aspx.cs" Inherits="reg.home" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. <script src="scripts/jquery-3.3.1.js"></script>  
  7. </head>  
  8. <body>  
  9. <form id="form1" runat="server">  
  10. <div>  
  11. <asp:TextBox ID="TextBox1" runat="server"  
  12. onkeyup="sayKeyCode(event,this.value);">  
  13. </asp:TextBox>  
  14. </div>  
  15. <asp:RegularExpressionValidator ID="rgx" ControlToValidate="TextBox1" runat="server"  
  16. ErrorMessage="only 2 digit is allowed before and after colon" ForeColor="Red"  
  17. Display="Dynamic"  
  18. ValidationExpression="^[0-9]{0,9}(\:[0-9]{2,2})?$"></asp:RegularExpressionValidator>  
  19. </form>  
  20. <script type="text/javascript" language="javascript">  
  21. function sayKeyCode(event, v)  
  22. {  
  23. var TextBox = document.getElementById('<%=TextBox1.ClientID%>');  
  24. //alert(event.keyCode);  
  25. if(event.keyCode != 8)  
  26. {  
  27. if (TextBox.value.length == 2 && TextBox.value.length != 3)  
  28. {  
  29. TextBox.value = TextBox.value + ":";  
  30. }  
  31. else  
  32. {  
  33. TextBox.value = TextBox.value;  
  34. }  
  35. }  
  36. }  
  37. </script>  
  38. </body>  
  39. </html>  

Answers (1)