67

I'm trying to use an ASP.NET RangeValidator to validate a date on a textbox. The format of the date entered on the textbox is dd MMMM yyyy.

How can I use the range validator to validate a valid date? If I enter 1 January 1000 as the min or max value I get an error saying value cannot be converted to type date, but if I use another format it picks up my entered text as invalid.

Below is my code:

<asp:TextBox 
    runat="server" 
    ID="txtDatecompleted" 
/>
<cc2:CalendarExtender
    ID="datecompletedExtender" 
    runat="server"
    TargetControlID="txtDatecompleted"
    Format="dd MMMM yyyy"
/>  
<asp:RangeValidator 
    runat="server" 
    ID="RangeValidator1" 
    Type="Date" 
    ControlToValidate="txtDatecompleted" 
    MaximumValue="9999/12/28" 
    MinimumValue="1000/12/28" 
    ErrorMessage="enter valid date" 
    Display="None"
/>
<cc2:ValidatorCalloutExtender 
    ID="RangeValidator1_ValidatorCalloutExtender" 
    runat="server"
    Enabled="True"
    TargetControlID="RangeValidator1">
</cc2:ValidatorCalloutExtender>
Welbog
  • 59,154
  • 9
  • 110
  • 123
Nicholas
  • 1,740
  • 5
  • 22
  • 31

4 Answers4

138

Best option would be

Add a compare validator to the web form. Set its controlToValidate. Set its Type property to Date. Set its operator property to DataTypeCheck eg:

<asp:CompareValidator
    id="dateValidator" runat="server" 
    Type="Date"
    Operator="DataTypeCheck"
    ControlToValidate="txtDatecompleted" 
    ErrorMessage="Please enter a valid date.">
</asp:CompareValidator>
David Clarke
  • 12,888
  • 9
  • 86
  • 116
Shoban
  • 22,920
  • 8
  • 63
  • 107
  • 4
    Simple and elegant. Plus the built in controls also check for valid/invalid leap year dates. 2/29/2011 says invalid but 2/29/2012 comes back valid. No need to hassle with fancy regular expressions! – Dillie-O Jan 25 '12 at 20:19
  • 1
    @Ryan Sammut I also want to know the answer to this question. The Date type CompareValidator accepts MM/DD/YY. I want to exclude the 2-digit year value. – danyim Sep 24 '12 at 15:53
  • 3
    Just a note that the CompareValidator is culture dependent, so if you would like another dateformat (e.g. dd-mm-yyyy) just set the culture accordinaly – Richard May 31 '13 at 08:36
  • 1
    This works, but the caveat is that this allows dates that may be out of range of SQL dates less than 1753 or you may have a date range (no future date entries). This does work, but Chris Van Opstal's may be needed for SQL date edits. – Charles Byrne Dec 04 '13 at 21:13
  • This won't work for the question asked because of the date format specified as "dd MMMM yyyy". CustomValidator on dates only works if the date is in "dd/mm/yyyy" format (for en-GB) or "mm/dd/yyyy" for (en-US). – Fandango68 Jun 30 '15 at 02:18
27

A CustomValidator would also work here:

<asp:CustomValidator runat="server"
    ID="valDateRange" 
    ControlToValidate="txtDatecompleted"
    onservervalidate="valDateRange_ServerValidate" 
    ErrorMessage="enter valid date" />

Code-behind:

protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args)
{
    DateTime minDate = DateTime.Parse("1000/12/28");
    DateTime maxDate = DateTime.Parse("9999/12/28");
    DateTime dt;

    args.IsValid = (DateTime.TryParse(args.Value, out dt) 
                    && dt <= maxDate 
                    && dt >= minDate);
}
Chris Van Opstal
  • 36,423
  • 9
  • 73
  • 90
  • I tried your method but it failed within an update panel. Pls chk: http://stackoverflow.com/questions/2071585/asp-net-custom-validation-not-working-even-when-required-field-validator-is-use/14361135#14361135 – Yoosaf Abdulla Jan 16 '13 at 14:55
  • 1
    This was the best way for me, easiest and i have total control over what is checked&done in the code behind. Thnx alot Chris!! – user1501127 Dec 01 '13 at 12:07
  • 1
    This works good for SQL date range edits for legacy systems that use the datetime fields as well. Thanks. – Charles Byrne Dec 04 '13 at 21:14
8

I think the following is the easiest way to do it.

<asp:TextBox ID="DateControl" runat="server" Visible="False"></asp:TextBox>
<asp:RangeValidator ID ="rvDate" runat ="server" ControlToValidate="DateControl" ErrorMessage="Invalid Date" Type="Date" MinimumValue="01/01/1900" MaximumValue="01/01/2100" Display="Dynamic"></asp:RangeValidator>
Cherian Paul
  • 81
  • 1
  • 1
2

I believe that the dates have to be specified in the current culture of the application. You might want to experiment with setting CultureInvariantValues to true and see if that solves your problem. Otherwise you may need to change the DateTimeFormat for the current culture (or the culture itself) to get what you want.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795