Validate date

  • Thread starter Thread starter Guest
  • Start date Start 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.
 
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
 
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.
 
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
 
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
 
Back
Top