0

I'm using a RegularExpressionValidator in ASP.NET. I have these validators for my asp:Textbox control:

<asp:RangeValidator
                ID="rvDate"
                runat="server"
                ControlToValidate="txtCrimeDate"
                Type="Date"
                ErrorMessage="Must be within latest three months"
                Display="Dynamic"
                OnInit="RangeValidator_Init">
            </asp:RangeValidator>
            <asp:RegularExpressionValidator
                ID="revDate"
                runat="server"
                ControlToValidate="txtCrimeDate"
                Display="Dynamic"
                ErrorMessage="Must be in format YYYY-MM-DD"
                ValidationExpression="[0-9]{4}-[0-9]{2}-[0-9]{2}">
            </asp:RegularExpressionValidator>

I have set the min and max value of the RangeValidator in the code-behind and it works as intended. But! If I enter something with 2 numbers for the year, like 15-11-28 everything passes, even the other validators for the other controls. Any ideas?

guitarzero
  • 545
  • 9
  • 18

1 Answers1

0

Check the date format (US or otherwise) and 0-9 for the day and month are incorrect eg:

2013-99-99

Look into using the CompareValidator control, this is mainly used for string matching, but can be made to check between two date ranges.

Mathemats
  • 1,185
  • 4
  • 22
  • 35
  • Yes @Mathemats, but that is already taken care of in the RangeValidator. I still don't understand how two numbers can pass, when I've said that it must be 4 before the "-"? – guitarzero Nov 29 '13 at 00:22
  • @user2989484 Better yet, http://stackoverflow.com/questions/939802/date-validation-with-asp-net-validator?rq=1 – Mathemats Nov 29 '13 at 00:58