How to validate date?

V

VC

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?
 
P

Peter Blum

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: [email protected]
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top