I'm trying to validate a textBox of dates in asp.net 2.0. (I have multiple dates to check, so I'm trying to make it as re-usable as possible.) I have copy/pasted the JavaScript from a one-field-only html page and checked that the code functions as intended. Below is the textbox code from the .net page. My problem is that entering 02/31/2007 doesn't fire the Javascript (although it did in the html page just fine) and even if I go back and change the entry to 01/31/2007 - I continue to get the errorMessage "Must be a valid date." Changing it to blank does fire the required field error message. I'm very new to validation, and would appreciate some help.
Thanks.
<asp:TextBox ID="rstart" runat="server" CausesValidation="true" />
<asp:RequiredFieldValidator ID="valRstart" ControlToValidate="rstart" runat="server" ErrorMessage=" Required field." />
<asp:RegularExpressionValidator ID="rvalRstart" ControlToValidate="rstart" runat="server" ErrorMessage=" Not valid" ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$" />
<asp:CustomValidator id="cvalRstart" runat="server" controlToValidate="rstart" ClientValidationFunction="check_date" OnServerValidate="dateValidate" ErrorMessage="Must be a valid date" />
<asp:button runat="server" ID="btnSubmit" Text="submit" />
Sub dateValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
Dim dt As DateTime, myControl As Control
myControl = source
If (DateTime.TryParse(args.Value, dt) = False) Or (dt <= DateTime.Today) Then
args.IsValid = False
myControl.Focus()
Else
args.IsValid = True
End If
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If Page.IsValid Then
....do my processing
End If
End Sub
Thanks.
<asp:TextBox ID="rstart" runat="server" CausesValidation="true" />
<asp:RequiredFieldValidator ID="valRstart" ControlToValidate="rstart" runat="server" ErrorMessage=" Required field." />
<asp:RegularExpressionValidator ID="rvalRstart" ControlToValidate="rstart" runat="server" ErrorMessage=" Not valid" ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$" />
<asp:CustomValidator id="cvalRstart" runat="server" controlToValidate="rstart" ClientValidationFunction="check_date" OnServerValidate="dateValidate" ErrorMessage="Must be a valid date" />
<asp:button runat="server" ID="btnSubmit" Text="submit" />
Sub dateValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
Dim dt As DateTime, myControl As Control
myControl = source
If (DateTime.TryParse(args.Value, dt) = False) Or (dt <= DateTime.Today) Then
args.IsValid = False
myControl.Focus()
Else
args.IsValid = True
End If
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If Page.IsValid Then
....do my processing
End If
End Sub