How to validate date?

Discussion in 'ASP .Net' started by VC, Mar 12, 2005.

  1. VC

    VC Guest

    I have a textbox where users type in a date. I want to check that the input
    is between year 2000 and the current date.
    Here is segment of the codes that give me the "Server tags cannot contain
    <%...%> construct"

    <asp:rangevalidator id="startDateValidator" runat="server" Type="Date"
    MinimumValue="1/1/2000"
    MaximumValue=<%=DateTime.Now.ToShortDateString()%>
    ..
    ..
    ..
    </asp:rangevalidator >

    Could anyone help me with codes/way to check whether a date input is within
    a range of value, preferably without using the code behind?
     
    VC, Mar 12, 2005
    #1
    1. Advertising

  2. VC

    Peter Blum Guest

    You almost have it right. You used the old <%= %> ASP style tags. They don't
    work inside of properties. DataBinding tags do: <%#
    Code:
     %>.
    <asp:rangevalidator id="startDateValidator" runat="server" Type="Date"
    MinimumValue="1/1/2000" MaximumValue=<%# DateTime.Now.ToShortDateString()%>
    But with databinding, you are required to call the DataBind() method on the
    control or the Page to apply it. That means you are going to use some code
    in Page_Load. Since that's required, I recommend assigning the property
    directly.
    Here's databinding:
    startDateValidator.DataBind();
    Here's assigning directly:
    startDateValidator.MaximumValue = DateTime.Now.ToShortDateString()
    
    The second case is much faster.
    
    Because so many users miss this issue when validating ranges, I wanted to
    point it out to you. The RangeValidator will not report an error until the
    text is a valid date format because it cannot compare an illegal date to the
    range. You should always have a validator for the format of the field. In
    this case, its Comparevalidator with Operator=DataTypeCheck and Type=Date.
    
    --- Peter Blum
    www.PeterBlum.com
    Email: 
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx
    
    "VC" <> wrote in message
    news:...[color=blue]
    >I have a textbox where users type in a date. I want to check that the input
    >is between year 2000 and the current date.
    > Here is segment of the codes that give me the "Server tags cannot contain
    > <%...%> construct"
    >
    > <asp:rangevalidator id="startDateValidator" runat="server" Type="Date"
    > MinimumValue="1/1/2000"
    > MaximumValue=<%=DateTime.Now.ToShortDateString()%>
    > .
    > .
    > .
    > </asp:rangevalidator >
    >
    > Could anyone help me with codes/way to check whether a date input is
    > within a range of value, preferably without using the code behind?
    >[/color]
     
    Peter Blum, Mar 12, 2005
    #2
    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:
    646
    Matthew Speed
    Nov 8, 2003
  2. James P.
    Replies:
    7
    Views:
    12,736
    phamtasmic
    Jul 13, 2004
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,366
    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:
    184
    Chris Davoli
    Jan 28, 2009
  5. Jay Pangmi
    Replies:
    4
    Views:
    704
    Suraj Kurapati
    Aug 25, 2008
Loading...

Share This Page