DatetimePicker:无效的正则表达式
我正在使用下面的正则表达式,它在日期验证的情况下工作正常。但是,如果我从 DatePicket 中选择日期,即使这样它也会抛出警报:输入有效日期。如何修改正则表达式以兼容DatePicker。
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "^(((0?[1-9]|[12]\d|3[01]).-\/.-\/?\d{2}))|((0?[1-9]|[12]\d|30).-\/.-\/?\d{2}))|((0?[1-9]|1\d|2[0-8])[.-\/]0?2.-\/?\d{2}))|(29[.-\/]0?2.-\/?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$"
.IgnoreCase = True
.Global = True
End With
expressionmatch = RegularExpressionObject.Test(TxtVal)
If expressionmatch Then
Else
msgbox "You must enter a valid Date.",,"Invalidentry"
form1.item(OHldr).focus()
Exit Function
End if
I am using the below Regular Expression which works fine in case of date validation. But if I select the date from the DatePicket even then it throws alert:Enter valid date. How can I modify the regular expression so as to be compatible with DatePicker.
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "^(((0?[1-9]|[12]\d|3[01]).-\/.-\/?\d{2}))|((0?[1-9]|[12]\d|30).-\/.-\/?\d{2}))|((0?[1-9]|1\d|2[0-8])[.-\/]0?2.-\/?\d{2}))|(29[.-\/]0?2.-\/?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$"
.IgnoreCase = True
.Global = True
End With
expressionmatch = RegularExpressionObject.Test(TxtVal)
If expressionmatch Then
Else
msgbox "You must enter a valid Date.",,"Invalidentry"
form1.item(OHldr).focus()
Exit Function
End if
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这不是您要问的,但有一种更简单的方法来验证日期时间。正则表达式很好,但有时有更简单的方法。
使用 CustomeValidator,并在 Server_Validate 事件中执行以下操作:
I know it's not what you're asking, but there's an easier way to validate datetimes. Regular Expressions are good, but sometimes there is an easier way.
Use a CustomeValidator, and in the Server_Validate event, do the following:
您可以使用
If Date.TryParse(s, d) Then ...
其中 s 是字符串,d 是日期。 date.tryparse 对于有效日期返回 true,对于无效日期返回 false。如果日期有效,则将其转换为 d 中的日期数据类型。You can use
If Date.TryParse(s, d) Then ...
where s is a string and d is a date. date.tryparse returns true for a valid date and false for an invalid date. If the date is valid, it converts it to a date datatype in d.