how to compare current date to a future date and validate it in ASP.NET

Discussion in 'ASP .Net' started by James P., Jul 10, 2004.

  1. James P.

    James P. Guest

    Hello there,

    In my asp.net page using VB, I have a date text field in mm/dd/yyyy
    format. When a date is entered, I'd like to validate it to make sure
    the date is greater than or equal to the current date. If not, I'd
    like to display the error message to ValidationSummary.

    It seems to make sense to me to use CompareValidator but the problem
    is put the current date into CompareValidator. So, I created a hidden
    text field in my aspx. In my VB code behind, I load current date to
    that text field. Then, I created a CompareValidator to compare this
    current date to the text field containing date entered by the user.
    In my Page_Load, I have Page.Validate to activate the validation.
    When I run it, the first round through, the validationSummary worked
    and displayed the warning message I wanted (that date entered can't be
    less than current date). However, it did not work after that. I
    think the problem is server validation did not work properly.

    My alternative way is to use client validation using CustomValidator.
    Pass in two date fields: current date and entered date and compare.
    Unfortunately, I don't know well neither javaScrip nor any other
    script languages to do it. The problem I am having with this approach
    is getting current date in the mm/dd/yyyy format so I can compare to
    what the user enters in that format. Date() function is javascrip
    returns a different format. And I think Now() returns both date and
    time.

    Any suggestions are greatly appreciated. Thank you in advance,
    James
     
    James P., Jul 10, 2004
    #1
    1. Advertising

  2. James P.

    Teemu Keiski Guest

    Hi,

    yes you'd use CompareValidator, but you need to give it the value as
    correctly formatted to the ValueToCompare property.

    First you'd put the validators something like this (I've omitted buttons and
    ValidationSummary to keep it concise):

    ***
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

    <asp:CompareValidator id="compDateDataTypeValidator"
    ControlToValidate="TextBox1" Operator="DataTypeCheck"
    Type="Date" runat="server" ErrorMessage="You must enter a valid
    date"></asp:CompareValidator>

    <asp:CompareValidator id="compDateValidator"
    ControlToValidate="TextBox1" Operator="LessThan" Type="Date"
    runat="server" ErrorMessage="Entered date must be less than current
    date"></asp:CompareValidator>
    ***

    Then additionally you'd set in code the ValueToCompare property for the
    CompareValidator (compDateValidator) which compares the dates. Like this:

    ***
    compDateValidator.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
    ***
    As you can see, it is given as string and the easiest way is to get it from
    DateTime.Now and then format using string formatting so that it passes for
    CompareValidator (standard date format based on current locale and culture)

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke







    "James P." <> wrote in message
    news:...
    > Hello there,
    >
    > In my asp.net page using VB, I have a date text field in mm/dd/yyyy
    > format. When a date is entered, I'd like to validate it to make sure
    > the date is greater than or equal to the current date. If not, I'd
    > like to display the error message to ValidationSummary.
    >
    > It seems to make sense to me to use CompareValidator but the problem
    > is put the current date into CompareValidator. So, I created a hidden
    > text field in my aspx. In my VB code behind, I load current date to
    > that text field. Then, I created a CompareValidator to compare this
    > current date to the text field containing date entered by the user.
    > In my Page_Load, I have Page.Validate to activate the validation.
    > When I run it, the first round through, the validationSummary worked
    > and displayed the warning message I wanted (that date entered can't be
    > less than current date). However, it did not work after that. I
    > think the problem is server validation did not work properly.
    >
    > My alternative way is to use client validation using CustomValidator.
    > Pass in two date fields: current date and entered date and compare.
    > Unfortunately, I don't know well neither javaScrip nor any other
    > script languages to do it. The problem I am having with this approach
    > is getting current date in the mm/dd/yyyy format so I can compare to
    > what the user enters in that format. Date() function is javascrip
    > returns a different format. And I think Now() returns both date and
    > time.
    >
    > Any suggestions are greatly appreciated. Thank you in advance,
    > James
     
    Teemu Keiski, Jul 10, 2004
    #2
    1. Advertising

  3. James P.

    Teemu Keiski Guest

    Sorry, I re-read your post and you said date format dd.mm.yyyy. TThen just
    change the format string to "dd/MM/yyyy" (or dd.MM.yyyy). /'s are considered
    culture-speficic separators to dates and will be replaced with dots.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke



    "Teemu Keiski" <> wrote in message
    news:...
    > Hi,
    >
    > yes you'd use CompareValidator, but you need to give it the value as
    > correctly formatted to the ValueToCompare property.
    >
    > First you'd put the validators something like this (I've omitted buttons

    and
    > ValidationSummary to keep it concise):
    >
    > ***
    > <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    >
    > <asp:CompareValidator id="compDateDataTypeValidator"
    > ControlToValidate="TextBox1" Operator="DataTypeCheck"
    > Type="Date" runat="server" ErrorMessage="You must enter a valid
    > date"></asp:CompareValidator>
    >
    > <asp:CompareValidator id="compDateValidator"
    > ControlToValidate="TextBox1" Operator="LessThan" Type="Date"
    > runat="server" ErrorMessage="Entered date must be less than current
    > date"></asp:CompareValidator>
    > ***
    >
    > Then additionally you'd set in code the ValueToCompare property for the
    > CompareValidator (compDateValidator) which compares the dates. Like this:
    >
    > ***
    > compDateValidator.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
    > ***
    > As you can see, it is given as string and the easiest way is to get it

    from
    > DateTime.Now and then format using string formatting so that it passes for
    > CompareValidator (standard date format based on current locale and

    culture)
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > ASP.NET Forum Moderator, AspAlliance Columnist
    > http://blogs.aspadvice.com/joteke
    >
    >
    >
    >
    >
    >
    >
    > "James P." <> wrote in message
    > news:...
    > > Hello there,
    > >
    > > In my asp.net page using VB, I have a date text field in mm/dd/yyyy
    > > format. When a date is entered, I'd like to validate it to make sure
    > > the date is greater than or equal to the current date. If not, I'd
    > > like to display the error message to ValidationSummary.
    > >
    > > It seems to make sense to me to use CompareValidator but the problem
    > > is put the current date into CompareValidator. So, I created a hidden
    > > text field in my aspx. In my VB code behind, I load current date to
    > > that text field. Then, I created a CompareValidator to compare this
    > > current date to the text field containing date entered by the user.
    > > In my Page_Load, I have Page.Validate to activate the validation.
    > > When I run it, the first round through, the validationSummary worked
    > > and displayed the warning message I wanted (that date entered can't be
    > > less than current date). However, it did not work after that. I
    > > think the problem is server validation did not work properly.
    > >
    > > My alternative way is to use client validation using CustomValidator.
    > > Pass in two date fields: current date and entered date and compare.
    > > Unfortunately, I don't know well neither javaScrip nor any other
    > > script languages to do it. The problem I am having with this approach
    > > is getting current date in the mm/dd/yyyy format so I can compare to
    > > what the user enters in that format. Date() function is javascrip
    > > returns a different format. And I think Now() returns both date and
    > > time.
    > >
    > > Any suggestions are greatly appreciated. Thank you in advance,
    > > James

    >
    >
     
    Teemu Keiski, Jul 10, 2004
    #3
  4. James P.

    James P. Guest

    "Teemu Keiski" <> wrote in message news:<uf#>...
    > Sorry, I re-read your post and you said date format dd.mm.yyyy. TThen just
    > change the format string to "dd/MM/yyyy" (or dd.MM.yyyy). /'s are considered
    > culture-speficic separators to dates and will be replaced with dots.
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > ASP.NET Forum Moderator, AspAlliance Columnist
    > http://blogs.aspadvice.com/joteke
    >
    >
    >
    > "Teemu Keiski" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > yes you'd use CompareValidator, but you need to give it the value as
    > > correctly formatted to the ValueToCompare property.
    > >
    > > First you'd put the validators something like this (I've omitted buttons

    > and
    > > ValidationSummary to keep it concise):
    > >
    > > ***
    > > <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    > >
    > > <asp:CompareValidator id="compDateDataTypeValidator"
    > > ControlToValidate="TextBox1" Operator="DataTypeCheck"
    > > Type="Date" runat="server" ErrorMessage="You must enter a valid
    > > date"></asp:CompareValidator>
    > >
    > > <asp:CompareValidator id="compDateValidator"
    > > ControlToValidate="TextBox1" Operator="LessThan" Type="Date"
    > > runat="server" ErrorMessage="Entered date must be less than current
    > > date"></asp:CompareValidator>
    > > ***
    > >
    > > Then additionally you'd set in code the ValueToCompare property for the
    > > CompareValidator (compDateValidator) which compares the dates. Like this:
    > >
    > > ***
    > > compDateValidator.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
    > > ***
    > > As you can see, it is given as string and the easiest way is to get it

    > from
    > > DateTime.Now and then format using string formatting so that it passes for
    > > CompareValidator (standard date format based on current locale and

    > culture)
    > >
    > > --
    > > Teemu Keiski
    > > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > > ASP.NET Forum Moderator, AspAlliance Columnist
    > > http://blogs.aspadvice.com/joteke
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > > "James P." <> wrote in message
    > > news:...
    > > > Hello there,
    > > >
    > > > In my asp.net page using VB, I have a date text field in mm/dd/yyyy
    > > > format. When a date is entered, I'd like to validate it to make sure
    > > > the date is greater than or equal to the current date. If not, I'd
    > > > like to display the error message to ValidationSummary.
    > > >
    > > > It seems to make sense to me to use CompareValidator but the problem
    > > > is put the current date into CompareValidator. So, I created a hidden
    > > > text field in my aspx. In my VB code behind, I load current date to
    > > > that text field. Then, I created a CompareValidator to compare this
    > > > current date to the text field containing date entered by the user.
    > > > In my Page_Load, I have Page.Validate to activate the validation.
    > > > When I run it, the first round through, the validationSummary worked
    > > > and displayed the warning message I wanted (that date entered can't be
    > > > less than current date). However, it did not work after that. I
    > > > think the problem is server validation did not work properly.
    > > >
    > > > My alternative way is to use client validation using CustomValidator.
    > > > Pass in two date fields: current date and entered date and compare.
    > > > Unfortunately, I don't know well neither javaScrip nor any other
    > > > script languages to do it. The problem I am having with this approach
    > > > is getting current date in the mm/dd/yyyy format so I can compare to
    > > > what the user enters in that format. Date() function is javascrip
    > > > returns a different format. And I think Now() returns both date and
    > > > time.
    > > >
    > > > Any suggestions are greatly appreciated. Thank you in advance,
    > > > James

    > >
    > >


    Teemu,

    Thanks a lot for responding and trying to help me. Yes, you got it
    right the first time with "mm/dd/yyyy" as I would like. I used your
    suggestion and I got "The value 'DateTime.Now.ToString("mm/dd/yyyy")'
    of the ValueToCompare property of 'cvDate' cannot be converted to type
    'Date'". Any other suggestions are appreciated.

    James
     
    James P., Jul 12, 2004
    #4
  5. James P.

    Teemu Keiski Guest

    If you see my VB code, it goes through compilation. You have a syntax typo
    there. What's *exactly* the code you use for this line?

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke



    "James P." <> wrote in message
    news:...
    > "Teemu Keiski" <> wrote in message

    news:<uf#>...
    > > Sorry, I re-read your post and you said date format dd.mm.yyyy. TThen

    just
    > > change the format string to "dd/MM/yyyy" (or dd.MM.yyyy). /'s are

    considered
    > > culture-speficic separators to dates and will be replaced with dots.
    > >
    > > --
    > > Teemu Keiski
    > > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > > ASP.NET Forum Moderator, AspAlliance Columnist
    > > http://blogs.aspadvice.com/joteke
    > >
    > >
    > >
    > > "Teemu Keiski" <> wrote in message
    > > news:...
    > > > Hi,
    > > >
    > > > yes you'd use CompareValidator, but you need to give it the value as
    > > > correctly formatted to the ValueToCompare property.
    > > >
    > > > First you'd put the validators something like this (I've omitted

    buttons
    > > and
    > > > ValidationSummary to keep it concise):
    > > >
    > > > ***
    > > > <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    > > >
    > > > <asp:CompareValidator id="compDateDataTypeValidator"
    > > > ControlToValidate="TextBox1" Operator="DataTypeCheck"
    > > > Type="Date" runat="server" ErrorMessage="You must enter a valid
    > > > date"></asp:CompareValidator>
    > > >
    > > > <asp:CompareValidator id="compDateValidator"
    > > > ControlToValidate="TextBox1" Operator="LessThan" Type="Date"
    > > > runat="server" ErrorMessage="Entered date must be less than

    current
    > > > date"></asp:CompareValidator>
    > > > ***
    > > >
    > > > Then additionally you'd set in code the ValueToCompare property for

    the
    > > > CompareValidator (compDateValidator) which compares the dates. Like

    this:
    > > >
    > > > ***
    > > > compDateValidator.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
    > > > ***
    > > > As you can see, it is given as string and the easiest way is to get it

    > > from
    > > > DateTime.Now and then format using string formatting so that it passes

    for
    > > > CompareValidator (standard date format based on current locale and

    > > culture)
    > > >
    > > > --
    > > > Teemu Keiski
    > > > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > > > ASP.NET Forum Moderator, AspAlliance Columnist
    > > > http://blogs.aspadvice.com/joteke
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > > "James P." <> wrote in message
    > > > news:...
    > > > > Hello there,
    > > > >
    > > > > In my asp.net page using VB, I have a date text field in mm/dd/yyyy
    > > > > format. When a date is entered, I'd like to validate it to make

    sure
    > > > > the date is greater than or equal to the current date. If not, I'd
    > > > > like to display the error message to ValidationSummary.
    > > > >
    > > > > It seems to make sense to me to use CompareValidator but the problem
    > > > > is put the current date into CompareValidator. So, I created a

    hidden
    > > > > text field in my aspx. In my VB code behind, I load current date

    to
    > > > > that text field. Then, I created a CompareValidator to compare this
    > > > > current date to the text field containing date entered by the user.
    > > > > In my Page_Load, I have Page.Validate to activate the validation.
    > > > > When I run it, the first round through, the validationSummary worked
    > > > > and displayed the warning message I wanted (that date entered can't

    be
    > > > > less than current date). However, it did not work after that. I
    > > > > think the problem is server validation did not work properly.
    > > > >
    > > > > My alternative way is to use client validation using

    CustomValidator.
    > > > > Pass in two date fields: current date and entered date and compare.
    > > > > Unfortunately, I don't know well neither javaScrip nor any other
    > > > > script languages to do it. The problem I am having with this

    approach
    > > > > is getting current date in the mm/dd/yyyy format so I can compare to
    > > > > what the user enters in that format. Date() function is javascrip
    > > > > returns a different format. And I think Now() returns both date and
    > > > > time.
    > > > >
    > > > > Any suggestions are greatly appreciated. Thank you in advance,
    > > > > James
    > > >
    > > >

    >
    > Teemu,
    >
    > Thanks a lot for responding and trying to help me. Yes, you got it
    > right the first time with "mm/dd/yyyy" as I would like. I used your
    > suggestion and I got "The value 'DateTime.Now.ToString("mm/dd/yyyy")'
    > of the ValueToCompare property of 'cvDate' cannot be converted to type
    > 'Date'". Any other suggestions are appreciated.
    >
    > James
     
    Teemu Keiski, Jul 12, 2004
    #5
  6. James P.

    phamtasmic Guest

    Teemu,

    Here is what I used:
    <asp:CompareValidator id="cvCompareDate" style="Z-INDEX: 115; LEFT:
    456px; POSITION: absolute; TOP: 180px"
    runat="server" ControlToValidate="txtFutureEventDate"
    ErrorMessage="toot toot" Operator="GreaterThanEqual"
    Type="Date"
    ValueToCompare='DateTime.Now.ToString("MM/dd/yyyy")'></asp:CompareValida
    tor>

    I even tried ("M/d/yyyy"). I got the same error message like I describe
    earlier.

    James

    *** Sent via Devdex http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    phamtasmic, Jul 12, 2004
    #6
  7. James P.

    Teemu Keiski Guest

    You can't assign the value on inline code that way. I used it in
    code-behind, but anyway, it should be in code (in your case put code in
    Page_Load, for example) to use it the way I demonstrated

    *** With the code ***
    cvCompareDate.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
    ******************

    Another way with inline way would be with binding. e.g set the attribute
    like this:

    ***With inline ***
    ValueToCompare='<%#DateTime.Now.ToString("MM/dd/yyyy")%>'
    ***************

    Additionally you would need to call DataBind for the CompareValidator
    control in question (or if suits, for the whole Page at the same time with
    Page.DataBind() ) to get this way to work
    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke

    "phamtasmic" <> wrote in message
    news:...
    >
    >
    > Teemu,
    >
    > Here is what I used:
    > <asp:CompareValidator id="cvCompareDate" style="Z-INDEX: 115; LEFT:
    > 456px; POSITION: absolute; TOP: 180px"
    > runat="server" ControlToValidate="txtFutureEventDate"
    > ErrorMessage="toot toot" Operator="GreaterThanEqual"
    > Type="Date"
    > ValueToCompare='DateTime.Now.ToString("MM/dd/yyyy")'></asp:CompareValida
    > tor>
    >
    > I even tried ("M/d/yyyy"). I got the same error message like I describe
    > earlier.
    >
    > James
    >
    > *** Sent via Devdex http://www.devdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Teemu Keiski, Jul 13, 2004
    #7
  8. James P.

    phamtasmic Guest

    Teemu,

    You are right about that. I put that code in the Init event for the
    textbox to get the current date and it worked.

    Thanks a lot for helping,
    James


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    phamtasmic, Jul 13, 2004
    #8
    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. CZ
    Replies:
    1
    Views:
    707
    Jack Li
    Sep 23, 2005
  2. Shailesh Katti

    compare validator to validate date in user control

    Shailesh Katti, Jul 27, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    764
    Teemu Keiski
    Jul 28, 2003
  3. Chris Davoli

    Validate Date AND Tme not just date

    Chris Davoli, Jan 28, 2009, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    178
    Chris Davoli
    Jan 28, 2009
  4. Michael Haas

    compare date field to current date

    Michael Haas, Feb 15, 2004, in forum: ASP General
    Replies:
    4
    Views:
    291
    Mike Haas
    Feb 15, 2004
  5. Scott Knapp

    Compare Input Date to Server Date

    Scott Knapp, Oct 2, 2003, in forum: Javascript
    Replies:
    2
    Views:
    188
    Dr John Stockton
    Oct 2, 2003
Loading...

Share This Page