Asp.net 正在验证日期时间上的日期
嗨,我正在尝试通过asp的文本框控件验证我的 我想让它显示日期是否为空,日期是今天还是将来而不是过去,并且日期格式是正确的MM/DD/YYY 我为此编写了以下代码:Asp.net 正在验证日期时间上的日期,asp.net,vb.net,Asp.net,Vb.net,嗨,我正在尝试通过asp的文本框控件验证我的 我想让它显示日期是否为空,日期是今天还是将来而不是过去,并且日期格式是正确的MM/DD/YYY 我为此编写了以下代码: If String.IsNullOrEmpty(txtPickupDate.Text) Then lblError.Text = lblError.Text & "<BR>You must enter in the field Pickup Date." lblError.
If String.IsNullOrEmpty(txtPickupDate.Text) Then
lblError.Text = lblError.Text & "<BR>You must enter in the field Pickup Date."
lblError.Visible = True
rtn = False
Else
If txtPickupDate.Text > DateTime.Now.ToString("MM/DD/YYYY") Then
lblError.Text = lblError.Text & "<BR>You must enter in a valid date for Pickup."
lblError.Visible = True
rtn = False
Else
Dim Pickup As Date
If Date.TryParseExact(txtPickupDate.Text.ToString(), "mm/dd/yyyy", _
System.Globalization.CultureInfo.CurrentCulture, _
Globalization.DateTimeStyles.None, Pickup) Then
Else
lblError.Text = lblError.Text & "<BR>You must enter the Pickup Date in the format of MM/DD/YYYY."
lblError.Visible = True
rtn = False
End If
End If
End If
如果String.IsNullOrEmpty(txtPickupDate.Text),则
lblError.Text=lblError.Text&“
您必须在字段中输入取货日期。”
lblError.Visible=True
rtn=错误
其他的
如果txtPickupDate.Text>DateTime.Now.ToString(“MM/DD/YYYY”),则
lblError.Text=lblError.Text&“
您必须输入有效的取货日期。”
lblError.Visible=True
rtn=错误
其他的
暗淡的拾取日期
如果Date.TryParseExact(txtPickupDate.Text.ToString(),“mm/dd/yyyy”_
System.Globalization.CultureInfo.CurrentCulture_
Globalization.DateTimeStyles.None,picking)然后
其他的
lblError.Text=lblError.Text&“
您必须以MM/DD/YYYY的格式输入取货日期。”
lblError.Visible=True
rtn=错误
如果结束
如果结束
如果结束
但是,它不适用于
如果txtPickupDate.Text>DateTime.Now.ToString(“MM/DD/YYYY”),则
试试这个。
它将验证是否输入了可解析的日期,然后仅检查日期部分,以确定它是否在过去、现在或将来
Dim enteredDate As DateTime
If Not DateTime.TryParse(txtPickupDate.Text, enteredDate) Then
' Invalid date entered.
Else
' Valid date entered.
' Validate against the date part only.
If enteredDate.Date < DateTime.Today Then
' Date is in the past.
Else
' Date is today or in the future.
End If
End If
Dim输入日期作为日期时间
如果不是DateTime.TryParse(txtPickupDate.Text,enteredDate),则
'输入的日期无效。
其他的
'输入有效日期。
'仅针对日期部分进行验证。
如果输入日期日期<日期时间.今天,则
“日期已经过去了。
其他的
“日期是今天还是将来。
如果结束
如果结束
我最终做了以下工作:
Dim Today As String = DateTime.Today.ToString("MM/dd/yyyy")
'Due Date
If String.IsNullOrEmpty(txtPickupDate.Text) Then
lblError.Text = lblError.Text & "<BR>You must enter in the field Pickup Date."
lblError.Visible = True
rtn = False
Else
If txtPickupDate.Text < Today Then
lblError.Text = lblError.Text & "<BR>You must enter in a valid date for Pickup Date."
lblError.Visible = True
rtn = False
Else
Dim Pickup As Date
If Date.TryParseExact(txtPickupDate.Text.ToString(), "MM/dd/yyyy", _
System.Globalization.CultureInfo.CurrentCulture, _
Globalization.DateTimeStyles.None, Pickup) Then
Else
lblError.Text = lblError.Text & "<BR>You must enter the Pickup Date in the format of MM/DD/YYYY."
lblError.Visible = True
rtn = False
End If
End If
End If
Dim Today As String=DateTime.Today.ToString(“MM/dd/yyyy”)
“到期日
如果String.IsNullOrEmpty(txtPickupDate.Text),则
lblError.Text=lblError.Text&“
您必须在字段中输入取货日期。”
lblError.Visible=True
rtn=错误
其他的
如果txtPickupDate.Text<今天,则
lblError.Text=lblError.Text&“
您必须输入有效的收货日期。”
lblError.Visible=True
rtn=错误
其他的
暗淡的拾取日期
如果Date.TryParseExact(txtPickupDate.Text.ToString(),“MM/dd/yyyy”_
System.Globalization.CultureInfo.CurrentCulture_
Globalization.DateTimeStyles.None,picking)然后
其他的
lblError.Text=lblError.Text&“
您必须以MM/DD/YYYY的格式输入取货日期。”
lblError.Visible=True
rtn=错误
如果结束
如果结束
如果结束
不要对日期数据进行文本比较。为什么不解析日期字符串?@puropoix,除非它是ISO 8601()格式,并且日期介于[1000到10000]之间。将txtPickupDate.Text
解析为一个日期,并将其与我尝试过的日期时间进行比较。parse(txtPickupDate.Text)>DateTime。现在,它仍然无法工作。谢谢我最终修复了它。不要使用字符串来比较数据。txtPickupDate.Text
对于“2015年1月31日”的Pickupdate,与2014年12月18日相比将失败#