Validate date that is in three separate input fields for Month, Day, and Year
I'm working on building a WebForm that has a set of Date of Birth fields at the bottom. This DOB is actually separated into three fields for Month, Day, and Year per the design of the page. We need to validate that the user is at least 13 years old when submitting the form.
If I had a single TextBox
, this would be easy with a RangeValidator
as such:
rvDOB.MaximumValue = DateTime.Now.AddYears(-13).ToShortDateString();
rvDOB.MinimumValue = DateTime.MinValue.ToShortDateString();
The issue is that I do not have a single TextBox
bu开发者_Python百科t three separate ones. How can I accomplish this check for at least 13 years with having three separate TextBox
controls?
One approach I tried was to create an additional TextBox
and when the form submission click event is raised, combine the three fields into the single textbox and validate on that. This did not work.
My approach would be to have an event fire after a value has been entered into each box, and then place some logic in that event which will validate the users age once each of the boxes has data in them.
delegate(object sender, EventArgs args) {
if (!box1.Text.IsNullOrEmpty() &&
!box2.Text.IsNullOrEmpty() &&
!box3.Text.IsNullOrEmpty()) {
// Validate the user is at least 13
}
}
精彩评论