Jump to content

Use the KeyPress event to restrict user entry for date format


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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...