Time validation

Create a web page in asp.net.Drag and drop a RegularExpressionValidator control on your ASP.NET page next to the control you want to validate and set it's ControlToValidate property to the ID of the TextBox.
Set ValidateExpression property : ^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$
1. Make sure AutoPostBack property of TextBox is True.

2. Write this code on TextChanged event handler of the TextBox.

Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim t As DateTime
If (DateTime.TryParse(TextBox1.Text, t) = True) Then
' Show your error message here.
End If
End Sub


Author: Narendra23 Jan 2010 Member Level: Bronze   Points : 1

Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys

Author: mausumi24 Jan 2010 Member Level: Gold   Points : 0


thanks it's very nice code.


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: