Jump to content

Use the KeyPress event to restrict user entry for date format


san1

Recommended Posts

HiI need to use the KeyPress eventto restrict date to MM/DD/YYYY format only. Here's my code:Private Sub txtDoj_KeyPress(ByVal sender As System.Object, ByVal e As System Windows.Forms.KeyPressEventArgs) Handles txtDoj.KeyPressIf Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = "." Thene.Handled = TrueEnd IfI understand that this code allows numbers only. But I do not know how to modify this to allow date format only.Thanks in advance for helping me out.

Link to comment
Share on other sites

That code is checking if each key is a digit, or a control character, or a period. You can change that to allow the characters you want, for example a slash instead of a period. I wouldn't use a keypress handler to check the format of the input though, I would probably use a blur handler so that you can check the format after they move away from the field (so that they are finished).

Link to comment
Share on other sites

The code that I shared is all I have to fulfill this because it has to be with KeyPress and the rest of the code in the click event.

 

I just asked for more clarity, perhaps a code example to guide me in the right direction. If you are going to be so blunt for no reason then I dont need your help. Thanks

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...