Reputation: 21
Hi i am trying to validate my through a textbox control for asp.
I want it to vaildate whether its empty, a date that is today or in the future not the past, and that the date format is correct MM/DD/YYY.
I have wrote this code for that:
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
However it does not work on the
If txtPickupDate.Text > DateTime.Now.ToString("MM/DD/YYYY") Then
Upvotes: 1
Views: 262
Reputation: 6568
Try this. It will validate that a parse-able date was entered and then check only the date portion for determining whether or not it is in the past, present, or future.
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
Upvotes: 1
Reputation: 21
I ended up doing this:
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
Upvotes: 0