Range Validator - Newby - Help:)

  • Thread starter Thread starter Chris D
  • Start date Start date
C

Chris D

Hi - I was hoping to use the range validator to check that the user's age is
between 10 & 65 based upon an entered DOB. I thought I could use max&min date
variables in the Validator & have those populated in the page_load event
based on today's date. Dead end so far. Please HELP! Many thanks ... c
 
Chris said:
Hi - I was hoping to use the range validator to check that the user's age is
between 10 & 65 based upon an entered DOB. I thought I could use max&min date
variables in the Validator & have those populated in the page_load event
based on today's date.

Yes, that sounds plausible.
Dead end so far. Please HELP! Many thanks ... c

Well, what did you try?
 
Hi Goran & thanks for helping. This is what I have ...
protected void Page_Load(object sender, EventArgs e)
{
DateTime theDate, maxDate, minDate;
theDate = DateTime.Parse(DateTime.Now);
minDate = theDate.AddYears(-65);
maxDate = theDate.AddYears(-10);
}

And this in the aspx
<asp:TextBox ID="tbDOBchk" runat="server"></asp:TextBox>
<asp:RangeValidator ID="rvDOBchk" Type="Date" runat="server"
ControlToValidate="tbDOBchk" ErrorMessage="RangeValidator"
MaximumValue="maxDate"
MinimumValue="minDate">message</asp:RangeValidator>
 
Chris said:
Hi Goran & thanks for helping. This is what I have ...
protected void Page_Load(object sender, EventArgs e)
{
DateTime theDate, maxDate, minDate;
theDate = DateTime.Parse(DateTime.Now);

The DateTime.Now property returns a DateTime value, so you should not
parse it. By doing so you force an implicit conversion to string, which
you then parse into a DateTime value identical to the one that
DateTime.Now returned.

theDate = DateTime.Now.

However, for your application the time is irrelevant, so you would
rather use the Today property:

theDate = DateTime.Today;
minDate = theDate.AddYears(-65);
maxDate = theDate.AddYears(-10);
}

And this in the aspx
<asp:TextBox ID="tbDOBchk" runat="server"></asp:TextBox>
<asp:RangeValidator ID="rvDOBchk" Type="Date" runat="server"
ControlToValidate="tbDOBchk" ErrorMessage="RangeValidator"
MaximumValue="maxDate"
MinimumValue="minDate">message</asp:RangeValidator>

You can't use variable names as property values. The control parses the
arguments, it doesn't evaluate them.

You should set the arguments from the Page_Load method:

rvDOBchk.MinimumValue = minDate;
rvDOBchk.MAximumValue = maxDate;
 
Göran Andersson said:
The DateTime.Now property returns a DateTime value, so you should not
parse it. By doing so you force an implicit conversion to string, which
you then parse into a DateTime value identical to the one that
DateTime.Now returned.

theDate = DateTime.Now.

However, for your application the time is irrelevant, so you would
rather use the Today property:

theDate = DateTime.Today;


You can't use variable names as property values. The control parses the
arguments, it doesn't evaluate them.

Also, the variables are local in the Page_Load method, so you would not
be able to reach them from the markup code.

Also, the Page_Load method runs after the markup is parsed, so even if
you could reach the variables, their values would not yet have been set.

So, in conclusion, the unreachable variables that you did not use
doesn't exist yet. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top