Validate date

G

Guest

I want to allow users to enter dates in a text box in either the US "MM dd
yy" format or the UK "dd MM yy" format.

But how can I validate these dates? All the date functions e.g. ISdate,
convert.ToDatetime etc fail on the US date format as the application and IIS
is set to UK format.

Its strange that the date format("12/03/06","MM dd yy") is interpretated
correctly but format("12/22/06","MM dd yy") bombs out.

Totally stuck on this.
 
R

Rad [Visual C# MVP]

Hey NH,

My advice is as much as possible to use the available components to
minimize your coding and validation. So for instance you could use a
calendar control or a datepicker and reduce your validation coding to
zero :)

However if you really have to use a textbox you face some challenges
1) Is the text entered a date at all? What if the person keyed in
"potatoes"
2) If the text is a date, is the date valid? What if they keyed in
30th February
3) Is the text a valid date depending on the context? US or UK
formats?

If I had no choice but to use text box, what I would do is this:

//
// Fetch our input date
//
string input = txtStartDate.Text;
//
//
// Declare a variable to store our date
//
DateTime parsedDate;
//
// Try to parse our text to a date, specifying
// the date is in UK format (d/M/yyyy)
//
if(DateTime.TryParse(input,
new System.Globalization.CultureInfo("en-gb"),
System.Globalization.DateTimeStyles.None,out parsedDate)){
//
// We succeeded! Now our date is in the
// parsedDate variable
//
MessageBox.Show(parsedDate.ToLongDateString());
}
else{
//
// We failed
//
MessageBox.Show(string.Format("Your entered date,{0} is
invalid", input));
}

Let me know if you need it in vb.net
 
M

Mark Rae

My advice is as much as possible to use the available components to
minimize your coding and validation. So for instance you could use a
calendar control or a datepicker and reduce your validation coding to
zero :)

Absolutely! I couldn't agree more!

And, in addition to that, make absolutely sure that users are not able to
enter dates ambiguously: ask an American to type in the date of Christmas
Day this year, and chances are you'll get 12/25/06 or 12/25/2006 - no use at
all to use Europeans, as there aren't 25 months in a year. However, forcing
either "25 Dec 2006" or "Dec 25 2006" provides total unambiguity under any
culture.
 
R

Rad [Visual C# MVP]

Hey NH,

Here you go, in VB

'
' Fetch our inputstring date
'
dim inputstring as string = txtStartDate.Text
'
'
' Declare a variable to store our date
'
dim parsedDate as DateTime
'
' Try to parse our text to a date, specifying
' the date is in UK format (d/M/yyyy)
'
if DateTime.TryParse(inputstring, _
new System.Globalization.CultureInfo("en-gb"), _
System.Globalization.DateTimeStyles.None,parsedDate) then
'
' We succeeded! Now our date is in the
' parsedDate variable
'
MessageBox.Show(parsedDate.ToLongDateString())
else
'
' We failed
'
MessageBox.Show(string.Format("Your entered date,{0} is
invalid", _
inputstring))
end if
 
G

Guest

thank you! problem solved.
NH

Rad said:
Hey NH,

Here you go, in VB

'
' Fetch our inputstring date
'
dim inputstring as string = txtStartDate.Text
'
'
' Declare a variable to store our date
'
dim parsedDate as DateTime
'
' Try to parse our text to a date, specifying
' the date is in UK format (d/M/yyyy)
'
if DateTime.TryParse(inputstring, _
new System.Globalization.CultureInfo("en-gb"), _
System.Globalization.DateTimeStyles.None,parsedDate) then
'
' We succeeded! Now our date is in the
' parsedDate variable
'
MessageBox.Show(parsedDate.ToLongDateString())
else
'
' We failed
'
MessageBox.Show(string.Format("Your entered date,{0} is
invalid", _
inputstring))
end if
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top