0

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; 

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks I tried RangeValidator but it not work (see edit post)
Answer updated with a note regarding MinimumValue and MaximumValue. If it doesn't solve your problem please give me more details.
That still not work, I think because the textbox will accept only FR date format (dd/mm/yyyy). Even if I use: rv.MinimumValue =DateTime.Now.ToString("yyyy/MM/dd");
If you need only one RangeValidator declare it via designer and set at runtime only its .MinimumValue/.MaximumValue (using Page_Load with IsPostBack = False). I try with a TextBox that accepts IT date format (dd/mm/yyyy) and everything is ok using your code to calculate minimum/maximum value.
Maybe you simply forget to add rv to your page controls; also using .ClientID instead of .ID seems not correct to me. When it's possible I usually put every controls that I need via designer to avoid these little problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.