Monday, May 21, 2012

Date Validation Using Range Validator

There may be instances that you want to restrict some range of dates in calendar extender. In this instance I’m restricting the future dates. (Current date is the maximum date !)  I have Calendar Extender and TextBox(DateBox) to select the date. When the user is selecting some date that exceeding current date, I want to give a restrict message.

 Insert the Range Validator component to the page.



Set properties as shown in following. Most important properties are
  • ControlToValidate – What is the control we want to compare with Range Validator (DateBox)
  • Type – Since we are going to validate Date, this should be Date type



  // set the default date in date box

DateBox.Text = DateTime.Today.ToShortDateString();
RangeValidator1.MaximumValue = DateTime.Today.ToShortDateString();

RangeValidator1.Validate();

if (RangeValidator1.IsValid)
// your code when date is valid.
else
  // your code when date is exceeding today.
Set the Minimum Value of the Range Validator in properties window or in page load event. Done!!

When the user is selecting a invalid date then RangeValidator1 is displaying and your custom code which is in commented area is working. Post your comments!




 

No comments:

Post a Comment