Apply Client Side Validation on DOB Selected by User

Introduction

 
In my previous articles I told you about:
  1. How to Determine Age Using Ajax Calendar in ASP.NET Application
  2. How to Calculate Age by Inserting DOB Manually.
  3. How to Apply Server-side Validation
This article explains how to apply client-side validation on a DOB selected by the user. Earlier articles clearly described how to calculate the age of the user but what if the user selects a date from the future? Or selects a date from 1800 or even earlier than that. We should create an application that should prevent the user from selecting such unnecessary dates, this can be done both by client-side validations or through server-side validations.
 
This article will cover only client-side validation, if you want to do server-side validation then you should follow my next article.
 
Step 1
 
I am working on my previous application so most of the code is the same. If you are a new reader then you must add two Textboxes to your application, one for getting the DOB from the user and other for showing the age.
  1. <asp:TextBox ID="txtDATE_OF_BIRTH" Onchange="return findage();" runat="server" Font-Size="12px" ></asp:TextBox>  
  2.    
  3.     <label id="Label34" runat="server" style="color:Gray; font-size:9px">Focus on Textbox to see Calender</label>  
  4.     </td>     
  5.         </tr>  
  6.         <tr>  
  7.             <td >  
  8.     <asp:Label ID="Label35" runat="server" CssClass="lbl1" Text="Age"></asp:Label>  
  9.     </td>  
  10.     <td >  
  11.     <asp:TextBox ID="txtCONSULTANT_AGE" runat="server" Font-Size="12px" ></asp:TextBox>  
  12.         </td> 
Here txtDATE_OF_BIRTH will be used to show the Calendar and txtCONSULTANT_AGE will be used for showing the age of the user.
 
Step 2
 
Now you should call an Ajax Calendar Extender.
  1. <cc1:CalendarExtender runat="server" TargetControlID="txtDATE_OF_BIRTH" Format="yyyy-MM-dd" OnClientDateSelectionChanged="SelectDate" ID="CalendarExtender1"></cc1:CalendarExtender>  
  2. Here OnClientDateSelectionChanged os the name of the function passed that is to be called for calculating the age.  
  3. Write this code in the Script Tag just below the Calendar Extender:  
  4.         <script type="text/javascript">  
  5.             function SelectDate(e) {  
  6.                 var PresentDay = new Date();  
  7.                 var dateOfBirth = e.get_selectedDate();  
  8.                 var months = (PresentDay.getMonth() - dateOfBirth.getMonth() +  
  9.  (12 * (PresentDay.getFullYear() - dateOfBirth.getFullYear())));  
  10.                 document.getElementById("txtCONSULTANT_AGE").value = Math.round(months / 12);  
  11.                 document.getElementById("txtCONSULTANT_AGE").readOnly = true;  
  12.             }  
  13.     </script> 
Now if you run your application then you will see that the Calendar is working properly and the age is also shown.
 
client side validation on DOB
 
But I had selected a date from the future and it will not show any type of error or anything else that can prevent me from doing that.
 
client side validation on DOB
 
Step 3
 
Now we will work on the validation part, for validation I will compare the date selected and the date of today, if the date selected is greater than today's date then an error message will be shown.
 
This code is used for comparing:
  1. if (dateOfBirth > PresentDay) {  
  2.         alert("error");  
  3.        document.getElementById("txtCONSULTANT_AGE").value = "You are Going to Born";  
You need to add this JavaScript code in the code written in Step 2, so your final code should be like this:
  1. <script type="text/javascript">  
  2.         function SelectDate(e) {  
  3.             var PresentDay = new Date();  
  4.             var dateOfBirth = e.get_selectedDate();  
  5.             var months = (PresentDay.getMonth() - dateOfBirth.getMonth() +  
  6.  * (PresentDay.getFullYear() - dateOfBirth.getFullYear())));  
  7.             document.getElementById("txtCONSULTANT_AGE").value = Math.round(months / 12);  
  8.             document.getElementById("txtCONSULTANT_AGE").readOnly = true;  
  9.             if (dateOfBirth > PresentDay) {  
  10.                 alert("error");  
  11.                 document.getElementById("txtCONSULTANT_AGE").value = "You are Going to Born";  
  12.             }  
  13.         }           
  14. </script> 
Now you can run your application and can see the output.
 
Output
 
Select a future date from your Calendar and an Error Message will be displayed on your WebPage.
 
client side validation on DOB
 
The second TextBox where the age was shown will also contain an unexpected value.
 
client side validation on DOB