ASP.NET - Conversion type - Asked By Rosie Buchanan on 19-May-12 08:46 PM

I want to see if the date entered into the dob textbox is up to 17 years on 12/31 of measurement year.  I'm checking the age when the page loads.  

I have a DOB textbox on the page (in bold).  When the user puts in a date (in question 3 in the second bold) section on the page, if they are over 17 years of age a message should appear.  When I hit the submit button I am getting

 Dim startdate As Date = CDate(TextBoxDOB.Text) in the code behind

Here are the files I'm working with:

Here's the client side (scroll down to see code behind)

<body>

</div>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="scriptmanager1" runat="server"> </asp:ToolkitScriptManager>
<div>

<table style="width:75%;" align="center" border="1">
<tr>
<td colspan="2" align="right" valign="top">
<table class="style6">
<tr>
<td>
<a href="CMSPortal.aspx"><img align="left" src="images/back5.jpeg" border="0"/></a></td>
<td rowspan="2">
<img src="images/CMS%20Logo.png"
style="width: 162px; height: 90px; float: right;" /></td>
</tr>
<tr>
<td align="left" valign="bottom">
<asp:TextBox ID="Dtetxt" runat="server" Height="21px" Width="155px"></asp:TextBox>
</td>
</tr>
</table>

</td>
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#2A6F81" >
<center class="style1">ADULT BMI</center></td>
</tr>
<tr>
<td class="style5" bgcolor="#D6E3BC">
MRID</td>
<td bgcolor="#D6E3BC" >
<input id="Text1" type="text" /></td>
</tr>
<tr>
<td class="style5" bgcolor="#D6E3BC">
Birthdate:
 DOB:
                     <asp:TextBox ID="TextBoxDOB" runat="server"></asp:TextBox>
                    <asp:TextBoxWatermarkExtender ID="TextBoxDOB_TextBoxWatermarkExtender" 
                        runat="server" Enabled="True" TargetControlID="TextBoxDOB" 
                        WatermarkText="MM/DD/YYYY">
                    </asp:TextBoxWatermarkExtender>>

&nbsp;</td>
</tr>
<tr>
<td class="style5" valign="top" bgcolor="#D6E3BC">
1. Was there an outpatient visit for this member during the measurement year (2011) or the year prior&nbsp; to the
measurement year?</td>
<td class="style5" align="left" bgcolor="#D6E3BC">
<asp:RadioButtonList ID="OutVisitRtb" runat="server"
RepeatDirection="Horizontal" AutoPostBack="True">
<asp:ListItem Value="Y">Yes</asp:ListItem>
<asp:ListItem Value="N">No</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="style7" bgcolor="#D6E3BC">
2. Is there a BMI (or BMI percentile for members under age 19) documented in the
medical record on or between 01/01/2010 - 01/01/2011?</td>
<td class="style7" valign="top" bgcolor="#D6E3BC">
<asp:RadioButtonList ID="BMIRbn" runat="server" RepeatDirection="Horizontal"
AutoPostBack="True">
<asp:ListItem Value="Y">Yes</asp:ListItem>
<asp:ListItem Value="N">No</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="style5" valign="top" bgcolor="#D6E3BC">
3. Date of BMI (or BMI percentile for members under age 19) </td>
<td bgcolor="#D6E3BC">
<asp:TextBox ID="BMIdte" runat="server" style="font-family: Tahoma"></asp:TextBox>
&nbsp;<img id="images1" src="images/calendar.gif" style="width: 29px; height: 29px" alt="calendar"
align="bottom" />
<asp:RangeValidator ID="RangeValidatorBMIDte" runat="server" ErrorMessage="RangeValidator">*</asp:RangeValidator>
<asp:ValidatorCalloutExtender ID="RangeValidatorBMIDte_ValidatorCalloutExtender"
runat="server" Enabled="True" TargetControlID="RangeValidatorBMIDte">
</asp:ValidatorCalloutExtender>

</td>
</tr>
<tr>
<td class="style5" valign="top" bgcolor="#D6E3BC">
4a. Enter BMI value
<br />
<br />
4b. Enter BMI percentile (only if member under age 19)</td>
<td class="style5" bgcolor="#D6E3BC">
<asp:TextBox ID="BMIVal" runat="server" AutoPostBack="True"></asp:TextBox>
%<br />
<asp:TextBox ID="BMIPerctxt" runat="server" AutoPostBack="True"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style5" valign="top" bgcolor="#D6E3BC">
5. If the member is female, was there a diagnosis of pregnancy in 2010 or 2011?</td>
<td class="style5" bgcolor="#D6E3BC">
<asp:RadioButtonList ID="GenderRbn" runat="server"
RepeatDirection="Horizontal" style="font-family: Tahoma">
<asp:ListItem Value="Y">Yes</asp:ListItem>
<asp:ListItem Value="N">No</asp:ListItem>
<asp:ListItem Value="M">X - Male</asp:ListItem>
</asp:RadioButtonList>(Select X if member is a male)
&nbsp;</td>
</tr>
<tr>
<td class="style5"colspan="2" valign="top" bgcolor="#D6E3BC">
Comments:&nbsp;<asp:TextBox ID="Commtxt" runat="server"
Height="166px" Width="730px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" valign="top" style="text-align: center" bgcolor="#D6E3BC">
&nbsp;<asp:Button ID="Button1" runat="server" Text="Submit"
style="font-family: Tahoma" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" Text="Clear"
OnClientClick="this.form.reset();return false;" style="font-family: Tahoma"/>
</td>
</tr>
</table>
<asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID="BMIdte"
WatermarkCssClass="watermarked" WatermarkText="MM/DD/YYYY">
</asp:TextBoxWatermarkExtender>
<asp:CalendarExtender ID="CalendarExtender1" TargetControlID="BMIdte" PopupButtonID="images1" runat="server">
</asp:CalendarExtender>
</div>
</form>
<p class="style5" style="text-align: center">
Complete Medical Services, LLC, Patient Care Quality Assessment</p>

</body>
</html>



Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load

Dtetxt.Text = Date.Now

' Set measurement date values on form load
Session("mYear") = Year(Now()) - 1 ' measurement year
Session("mYearPrior") = Session("mYear") - 1 ' year prior to measurement year
Session("julyPrior") = "7/1/" + Session("mYearPrior").ToString ' july of year prior to measurement year
Session("junemYear") = "6/30/" + Session("mYear").ToString ' june of measurement year
Session("marchPrior") = "3/1/" + Session("mYearPrior").ToString ' march of year prior to measurement year
Session("febmYear") = "2/28/" + Session("mYear").ToString ' february of measurement year
Session("mYearStart") = "1/1/" + Session("mYear").ToString ' measurement year start
Session("mYearEnd") = "12/31/" + Session("mYear").ToString ' measurement year end

End Sub

Protected Sub TextBoxDOB_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxDOB.TextChanged
' criteria is that they turn 17 during the measurement year
' if the patient is 17 already at the beginning of the measurement year
' or the patient turns 17 during the measurement year the criteria is met

' test for being 17 at beginning of measurement year
Dim enddate As Date = CDate(Session("mYearStart"))
Dim startdate As Date = CDate(TextBoxDOB.Text)
Dim difference = enddate.Subtract(startdate)
' number of years old at start of measurement year. if 17 at the beginning of the measurement year all is good
Dim nyearsold As Integer = difference.Days / 365
' if not 17 at beginning of measurement year, check to see if 17 during the measurement year
If nyearsold <= 17 Then
' next get 17 years from birth year. if that equals measurement year all is good.
' if not display a message indicating so
If Year(startdate) + 17 <= Session("mYear") Then
Dim sReturn As String
sReturn = "Patient Must be 17 years old during the measurement year"
Response.Write("<script >alert('" & sReturn & "');</script>")
End If
End If
End Sub

Is this the right way to check the textbox to see if the date entered is over 17 years of age?


RAJASEKHAR RAJENDRAN replied to Rosie Buchanan on 22-May-12 03:14 AM
Rosie,

You can achieve this using the Custom Validator as below.

<asp:TextBox ID="dob" runat="server"></asp:TextBox>
  <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Invalid Age, maximum age is 17 years!"
  ClientValidationFunction="agecheck" ControlToValidate="dob"></asp:CustomValidator>

The Validator Function is as below.

</body>
 
<script type="text/javascript" language="javascript">
  function agecheck(sender, args) {
    var dt = new Date();
    var maxdate = new Date("December 31, 2011");
    var dob = new Date(document.getElementById('<%=dob.ClientID %>').value);
    var years = maxdate.getFullYear() - dob.getFullYear();
 
    //check to see if less than 17 years old
    if (years < 17 && years >= 0) {
      args.IsValid = true;
      return;
    }
    //if exactley 17 years old check to see if birthday month has passed
    if (years == 17) {
      if (dt.getMonth() + 1 > dob.getMonth() + 1) {
        args.IsValid = true;
        return;
      }
      //if the months are equal check to see if birthday day has passed
      if (dt.getMonth() + 1 == dob.getMonth() + 1) {
        if (dt.getDate() > dob.getDate()) {
          args.IsValid = true;
          return;
        }
      }
    }
    args.IsValid = false;
  }
</script> You can make your Own Logic in calculating the Age and Verify in the above. Thanks & Regards, Rajasekhar.R