Reputation: 33
This question may be difficult to convey. The goal is to make sure that the date inputted into the textbox is at least 18 years from the date of birth.
I have a textbox:
<asp:TextBox ID="DateFrom" runat="server"/>
I have a rangevalidator:
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="DateFrom" ErrorMessage="Age has to be more than 18 years" Type="Date"></asp:RangeValidator>
and I have an entity object:
rvtxtExecutiveDateTo.MinimumValue = (insiderObj.Insider.dateBirth ?? DateTime.MinValue).ToString("MM/dd/yyyy");
I have set the range validator in the code behind to:
RangeValidator1.MinimumValue = (insiderObj.Insider.dateBirth ?? DateTime.MinValue.AddYears(-18)).ToString("MM/dd/yyyy");
Upvotes: 0
Views: 1144
Reputation: 241525
The problem would appear to be with placement of the AddYears
method call.
You have:
(insiderObj.Insider.dateBirth ?? DateTime.MinValue.AddYears(-18))
You probably meant:
(insiderObj.Insider.dateBirth ?? DateTime.MinValue).AddYears(-18)
Of course, this assumes that dateBirth
is a nullable value (DateTime?
). Otherwise, there'd be no reason to have ?? DateTime.MinValue
at all.
Also, you should not hardcode the MM/dd/yyyy
format in the ToString
call. Per the MSDN documentation:
Note
If you specify
ValidationDataType.Date
for theBaseCompareValidator.Type
property without programmatically setting the culture for the application, you should use a culture-neutral format, such asYYYY/MM/DD
, for theMaximumValue
andMinimumValue
properties. Otherwise, the date may not be interpreted correctly.
Either switch your format to "yyyy/MM/dd"
, or configure your application to be culture-aware and either pass CultureInfo.CurrentCulture
or omit the parameter entirely.
Upvotes: 0