Complex CompareValidator Validations

J

John Walker

Hi,
I have a datagrid with two ASP:TextBox TemplateColumns in which the user
will enter MM/DD/YYYY dates. In one column, the date is not allowed to be
more than 7 days into the future; and in the other column, the date must be
less than or equal to today's date. We would like this to be client side
validation. Is it possible to handle these types of comparisons with
ASP:CompareValidator? Or any other validator?

Thanks,
John
 
J

John Walker

Peter,

This works great! Thanks!

John

Peter Blum said:
Yes, the CompareValidator with Type=Date works.
To set 7 days in the future:
CompareValidator1.ValueToCompare =
DateTime.Today.AddDays(7).ToShortDateString()
To set today:
CompareValidator2.ValueToCompare = DateTime.Today.ToShortDateString()

Additionally, add a CompareValidator to confirm that the text is actually a
valid date format so an entry like 99/99/9999 is considered invalid. The
ValueToCompare feature only works against valid dates so the second
validator is necessary.
CompareValidator with Type=Date and Operator=DataTypeCheck.

Even though this will provide client-side validation, you should ALWAYS
setup server side validation because the validators only provide client-side
validation on IE and IE/Mac and javascript can be turned off. (My
Professional Validation And More has validators that support many more
browsers.)

Its very easy to get server side validation. In the Click post back event
method, test Page.IsValid is true before saving.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
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

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top