tags:

views:

40

answers:

1

I have been using this to take date as mm/dd/yyyy format...

<asp:CompareValidator ErrorMessage="(mm/dd/yyyy)" Display="Dynamic" ID="valcDate"
    ControlToValidate="txtDob" Operator="DataTypeCheck" Type="Date"     
    runat="server"></asp:CompareValidator>
<asp:RangeValidator ID="valrDate" runat="server" ControlToValidate="txtDob"  
    MinimumValue="12/31/1950"
    MaximumValue="1/1/2100" Type="Date" Text="Invalid Date" Display="Dynamic" />

but it is taking the two digit year also... plz suggest

+2  A: 

You might want to use a regular expression for the date :

<asp:RegularExpressionValidator ID="dateValRegex" runat="server" ControlToValidate="txtDob" ErrorMessage="Please Enter a valid date in the format (mm/dd/yyyy)" ValidationExpression="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"></asp:RegularExpressionValidator>

Also, as Daniel pointed out below you will need to use this in conjunction with your other validators to fully validate the date.

Paulie Waulie
+1 might want to add the full code for the RegularExpressionValidator and also explain that this is required as well as the range validator, because this regex alone would allow an invalid date such as 02/31/2010
Daniel Dyson
Good point, I've updated the answer to include the validator code.
Paulie Waulie