Apply Server Side Validation on DOB Selected by User


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 Client-side Validation on DOB Selected by User.
This article will explain how to apply server-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 stop the user from selecting invalid dates. This can be done both by client-side validations or through server-side validations. This article will cover only server-side validation, if you want to have client-side validation then you should follow my previous article.
Step 1
I am working on my previous application so most of the code is the same. If you are a new viewer then you must add two Textboxes and a Button to your application. First the TextBox for getting the DOB from the user and the other for showing the age.
  1. <asp:TextBox ID="txtDATE_OF_BIRTH"  runat="server" Font-Size="12px" ></asp:TextBox>  
  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>  
  13. ;/tr>  
  14.          <tr>  
  15.          <td>  
  16.              <asp:Button runat="server" Text="Submit"></asp:Button>  
  17.          </td>  
  18.      </tr> 
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> 
Here in OnClientDateSelectionChanged the name of the function is passed to be called for calculating the age.
Write this code in the Script Tag just below the Calendar Extender:
  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. (12 * (PresentDay.getFullYear() - dateOfBirth.getFullYear())));  
  7.                 document.getElementById("txtCONSULTANT_AGE").value = Math.round(months / 12);  
  8.                 document.getElementById("txtCONSULTANT_AGE").readOnly = true;  
  9.             }  
  10. </script> 
Now if you run your application then you will see that the calendar is working properly and the age is also shown:
Server 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.
Server side validation on DOB
Step 3
Now we will work on the validation part, for validation you need to add a Range Validator to your application.
  1. <asp:RangeValidator Display="Dynamic" ID="RangeValidator1" ForeColor="Red"  
  2.    ControlToValidate="txtDATE_OF_BIRTH" runat="server"  
  3.    ErrorMessage="Wrong Date Selected" MinimumValue="1950-01-01">  
  4. </asp:RangeValidator> 
In this Range Validator, I provided the ForeColor, ControlToValidate, and MinimumValue. The minimum value is the earliest date the user can select.
The maximum date is not provided because in the Maximum Date I will pass a date on which the user is filling in the form, this can be done through the .aspx.cs page of our application, so just go to the .aspx.cs page and write this code in the Page Load:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  4.             RangeValidator1.MaximumValue = System.DateTime.Today.ToString("yyyy-MM-dd");  
Now it will use today's date as the Maximum Date.
Now your application is ready to be executed.
Now I will select a date greater than today's date.
Server side validation on DOB
As I click on the button our validation acts to show the error message.
Server side validation on DOB