dateTime.tryParse validation problem

Joined
Nov 8, 2006
Messages
5
Reaction score
0
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
 
Joined
Nov 8, 2006
Messages
5
Reaction score
0
Found the following regular expression from Saurabh Nath. The only thing it doesn't do is make sure the date is greater than today, but that's fine. I had tried about four claiming to check for leap year from regExpLib.com, but this is the first one I got to work. Many thanks to Saurabh.

((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(11|0?[469])([/])(30|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(2[0-8]|1[0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(29)([/])([3579][26]00)$)|(^(0?2)([/])(29)([/])([1][89][0][48])$)|(^(0?2)([/])(29)([/])([2-9][0-9][0][48])$)|(^(0?2)([/])(29)([/])([1][89][2468][048])$)|(^(0?2)([/])(29)([/])([2-9][0-9][2468][048])$)|(^(0?2)([/])(29)([/])([1][89][13579][26])$)|(^(0?2)([/])(29)([/])([2-9][0-9][13579][26])$))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top