Validate date

Discussion in 'ASP .Net' started by =?Utf-8?B?Tkg=?=, Nov 28, 2006.

  1. 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.
     
    =?Utf-8?B?Tkg=?=, Nov 28, 2006
    #1
    1. Advertising

  2. 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

    On Tue, 28 Nov 2006 09:02:02 -0800, NH <>
    wrote:

    >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.

    --

    Bits.Bytes.
    http://bytes.thinkersroom.com
     
    Rad [Visual C# MVP], Nov 28, 2006
    #2
    1. Advertising

  3. =?Utf-8?B?Tkg=?=

    Mark Rae Guest

    "Rad [Visual C# MVP]" <> wrote in message
    news:...

    > 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.
     
    Mark Rae, Nov 28, 2006
    #3
  4. 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

    On Wed, 29 Nov 2006 01:16:04 -0800, NH <>
    wrote:

    >Thanks for the info. I have a calendar picker in the app, its just that the
    >users want the option to be able to type in a date also. Users eh?!
    >
    >Do you have this code in VB?
    >
    >Thanks
    >N
    >
    >"Rad [Visual C# MVP]" wrote:
    >
    >> 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
    >>
    >> On Tue, 28 Nov 2006 09:02:02 -0800, NH <>
    >> wrote:
    >>
    >> >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.

    >> --
    >>
    >> Bits.Bytes.
    >> http://bytes.thinkersroom.com
    >>

    --

    Bits.Bytes.
    http://bytes.thinkersroom.com
     
    Rad [Visual C# MVP], Dec 1, 2006
    #4
  5. =?Utf-8?B?Tkg=?=

    Mark Rae Guest

    "Rad [Visual C# MVP]" <> wrote in message
    news:...

    > MessageBox.Show(parsedDate.ToLongDateString())


    Obviously, this will need to be a JavaScript alert instead...
     
    Mark Rae, Dec 1, 2006
    #5
  6. Whoops, an artefact from my little test routine ....

    On Fri, 1 Dec 2006 20:40:18 -0000, "Mark Rae" <>
    wrote:

    >"Rad [Visual C# MVP]" <> wrote in message
    >news:...
    >
    >> MessageBox.Show(parsedDate.ToLongDateString())

    >
    >Obviously, this will need to be a JavaScript alert instead...
    >

    --

    Bits.Bytes.
    http://bytes.thinkersroom.com
     
    Rad [Visual C# MVP], Dec 1, 2006
    #6
  7. thank you! problem solved.
    NH

    "Rad [Visual C# MVP]" wrote:

    > 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
    >
    > On Wed, 29 Nov 2006 01:16:04 -0800, NH <>
    > wrote:
    >
    > >Thanks for the info. I have a calendar picker in the app, its just that the
    > >users want the option to be able to type in a date also. Users eh?!
    > >
    > >Do you have this code in VB?
    > >
    > >Thanks
    > >N
    > >
    > >"Rad [Visual C# MVP]" wrote:
    > >
    > >> 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
    > >>
    > >> On Tue, 28 Nov 2006 09:02:02 -0800, NH <>
    > >> wrote:
    > >>
    > >> >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.
    > >> --
    > >>
    > >> Bits.Bytes.
    > >> http://bytes.thinkersroom.com
    > >>

    > --
    >
    > Bits.Bytes.
    > http://bytes.thinkersroom.com
    >
     
    =?Utf-8?B?Tkg=?=, Dec 4, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    608
    Matthew Speed
    Nov 8, 2003
  2. James P.
    Replies:
    7
    Views:
    12,522
    phamtasmic
    Jul 13, 2004
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,278
    Michael Borgwardt
    May 30, 2004
  4. Chris Davoli

    Validate Date AND Tme not just date

    Chris Davoli, Jan 28, 2009, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    172
    Chris Davoli
    Jan 28, 2009
  5. Jay Pangmi
    Replies:
    4
    Views:
    666
    Suraj Kurapati
    Aug 25, 2008
Loading...

Share This Page