如何验证输入到文本框中的日期,以确保其格式正确,如果不显示消息。 [英] how to validate date entered into text box to make sure its right format if not show message.
问题描述
我有一个数据库,其中一列有日期。允许空值。尝试使用DateTimePicker并意识到不允许null。所以我在DateTimePicker上放了一个文本框。当我从DateTimePicker日历中选择时,它会将
日期输入到文本框中。但我希望能够直接输入日期并将其验证为2018年10月24日格式,因此如果有人输入24/10/18或24/10/2018,则接受其他显示消息。我正在使用VB.net而不是C.请感谢任何帮助
这是一个新的VB。谢谢
您好mkj26,
根据您的描述,您似乎可以通过Regex实现它。这里有一个vb.net示例供您参考。
Private Sub TextBox1_Validating(sender As Object,e As System.ComponentModel.CancelEventArgs)处理TextBox1.Validating
Dim regString As String =" ^(0 [1-9] | [1-2] [0-9] | 3 [0-1]) - [A-Za-z] {3} - [1- 9] \d {3}
"
如果不是Regex.IsMatch(TextBox1.Text,regString)那么
MessageBox.Show(" Error")
End if
End Sub
祝你好运,
张龙
I have a database that has date in one of the columns. Null value is allowed. Tried using DateTimePicker and realized that null is not allowed. So I have put a text box over the DateTimePicker. When I select from the DateTimePicker calender it inputs the date into the textbox. But I want to have the ability to enter the date directly and have it validated to be 24-Oct-2018 format so if someone types 24/10/18 or 24/10/2018 it is accepted else to show message. I am using VB.net not C. Would appreciate any help on this being a new to VB. Thanks
Hi mkj26,
According to your description, it seems that you could achieve it via Regex. here is a vb.net sample for your reference.
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating Dim regString As String = "^(0[1-9]|[1-2][0-9]|3[0-1])-[A-Za-z]{3}-[1-9]\d{3}
" If Not Regex.IsMatch(TextBox1.Text, regString) Then MessageBox.Show("Error") End If End SubBest regards,
Zhanglong
这篇关于如何验证输入到文本框中的日期,以确保其格式正确,如果不显示消息。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!