TimeIsNear
TimeIsNear

Reputation: 755

How to use CompareOperator Validator to compare value between two others values

I need to compare date in textbox between today N-1 and today. For example 19/02/15, date should be between 19/02/14 and 19/02/15 (sorry, date format FR)

For greater than (year-1) is ok:

txtDate.ValueToCompare = DateTime.Today.AddYears(-1).AddDays(-1).ToShortDateString();

txtDate.CompareOperator = CompareOperator.GreaterThanEqual;

How can I check that not greater than today?

Thanks in advance.

EDIT:

I tried this but not work:

            RangeValidator rv = new RangeValidator();
            rv.ControlToValidate = txtDate.ClientID;
            rv.Type = ValidationDataType.Date;
            rv.MinimumValue = DateTime.Today.AddYears(-1).AddDays(-1).ToShortDateString();
            rv.MaximumValue = DateTime.Today.ToShortDateString();
            rv.SetFocusOnError = true;

Upvotes: 0

Views: 114

Answers (1)

tezzo
tezzo

Reputation: 11115

In ASP.NET you can also use a RangeValidator that checks whether the value of an input control is within a specified range of values.

As stated in MSDN documentation please remember that if you specify ValidationDataType.Date for the BaseCompareValidator.Type property without programmatically setting the culture for the application, you should use a culture-neutral format, such as YYYY/MM/DD, for the MaximumValue and MinimumValue properties. Otherwise, the date may not be interpreted correctly.

Upvotes: 1

Related Questions