הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, March 19, 2014 3:53 AM
Hi
I 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.KeyPress
If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = "." Then
e.Handled = True
End If
I understand that this code allows numbers only. But I do not know how to modify this to allow date format only.
NOTE: I'm not allowed to use built-in data routines or functions; i cannot use datepicker
Thanks in advance for helping me out.
All replies (4)
Thursday, March 20, 2014 7:18 AM ✅Answered
Hi,
First, the best way is using DateTimePicker, it has a lot methods to check the data and limiting the selection.
If you still want to use a textbox to get that data, I would suggest you have a look at the following tips.
Using KeyPress or TextChanged event will check each char you are typing, that could not tell whether you have finished typing the vaule.
I.E, in that case using the method shared by you to limit typing numbers only, if we type "1.1........" it could not tell this is invalid data, so I would suggest you consider the following way to notice the user this is an invalid data.
In this case, we need a TextBox and a label.
Imports System.Text.RegularExpressions
Public Class Form1
Dim dateReg As Regex = New Regex("((^(10|12|0?[13578])(/)(3[01]|[12][0-9]|0?[1-9])(/)((1[8-9]\d{2})|([2-9]\d{3}))$)" _
& "|(^(11|0?[469])(/)(30|[12][0-9]|0?[1-9])(/)((1[8-9]\d{2})|([2-9]\d{3}))$)" _
& "|(^(0?2)(/)(2[0-8]|1[0-9]|0?[1-9])(/)((1[8-9]\d{2})|([2-9]\d{3}))$)" _
& "|(^(0?2)(/)(29)(/)([2468][048]00)$)|(^(0?2)(/)(29)(/)([3579][26]00)$)" _
& "|(^(0?2)(/)(29)(/)([1][89][0][48])$)" _
& "|(^(0?2)(/)(29)(/)([2-9][0-9][0][48])$)" _
& "|(^(0?2)(/)(29)(/)([1][89][2468][048])$)" _
& "|(^(0?2)(/)(29)(/)([2-9][0-9][2468][048])$)" _
& "|(^(0?2)(/)(29)(/)([1][89][13579][26])$)" _
& "|(^(0?2)(/)(29)(/)([2-9][0-9][13579][26])$))")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.ForeColor = Color.Red
Label1.Text = ""
End Sub
Private Sub txtDoj_TextChanged(sender As Object, e As EventArgs) Handles txtDoj.TextChanged
Dim dateMatch As Match = dateReg.Match(txtDoj.Text.Trim())
If dateMatch.Success Then
Label1.Text = ""
Else
Label1.Text = "Invalid Data"
End If
End Sub
End Class
In addition, you could also code like the following way to check the value.
If Date.TryParseExact(txtDoj.Text.Trim, "MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None, New Date) Then
Label1.Text = ""
Else
Label1.Text = "Invalid Data"
End If
Regards.
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Thursday, March 20, 2014 8:29 AM ✅Answered | 1 vote
Use a dateTime Picker.
This kind of solutions leads always to the same problems with end users, although for one you can wait a while if you don't have to edit dates in the past and that is the millennium bug.
And if you want to use a control which is not made for date times, then simply test it when the user leaves the control on a valid date.
Success
Cor
Thursday, March 20, 2014 9:36 AM ✅Answered | 1 vote
I don`t know if this is a coincidence but, this same question with the same Sub txtDoj_KeyPress event was asked yesterday by someone with a different forum name at this LINK. The DateTimePicker would be the best way to let a user select a Date. They would not be able to enter an invalid Date.
Wednesday, March 19, 2014 4:21 AM
KeyPress event of what kind of control?