Compare Age between two Dates using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

i want to validate a used for registration if he is  between 16 to 25 years before or on a given date suppose 30/11/2018 so how can i validate in c# if his age is 25 year and 1 day then dont allow to submit form how can i achive this?

Posted one month ago

Hiiammann,

Check this example. Now please take its reference and correct your code and restrict form submition.

HTML

Date Of Borth:<asp:TextBox ID="txtDOB" runat="server" Text="11/30/1993" />
<br />
Current Date:<asp:TextBox ID="txtDate" runat="server" Text="11/30/2018" />
<asp:Button Text="Sign Up" runat="server" OnClick="SignUp" />

C#

protected void SignUp(object sender, EventArgs e)
{
    DateTime dob = Convert.ToDateTime(txtDOB.Text);
    DateTime dateToValidate = Convert.ToDateTime(txtDate.Text);
    TimeSpan ts = dateToValidate - dob;
    DateTime age = DateTime.MinValue + ts;
    int years = age.Year - 1;
    int months = age.Month - 1;
    int days = age.Day - 1;
    string totalAge = String.Format("{0} year/s {1} month/s {2} day/s old", years, months, days);
    if (years >= 16 && years <= 25)
    {
        if (years == 25)
        {
            if (months == 0 && days == 0)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Validation Success\\nYou are " + totalAge + "')", true);
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Validation Failed\\nYou are " + totalAge + "')", true);
            }
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Validation Success\\nYou are " + totalAge + "')", true);
        }
    }
    else
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Validation Failed\\nYou are " + totalAge + "')", true);
    }
}

VB.Net

Protected Sub SignUp(ByVal sender As Object, ByVal e As EventArgs)
    Dim dob As DateTime = Convert.ToDateTime(txtDOB.Text)
    Dim dateToValidate As DateTime = Convert.ToDateTime(txtDate.Text)
    Dim ts As TimeSpan = dateToValidate - dob
    Dim age As DateTime = DateTime.MinValue + ts
    Dim years As Integer = age.Year - 1
    Dim months As Integer = age.Month - 1
    Dim days As Integer = age.Day - 1
    Dim totalAge As String = String.Format("{0} year/s {1} month/s {2} day/s old", years, months, days)
    If years >= 16 AndAlso years <= 25 Then
        If years = 25 Then
            If months = 0 AndAlso days = 0 Then
                ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Validation Success\nYou are " & totalAge & "')", True)
            Else
                ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Validation Failed\nYou are " & totalAge & "')", True)
            End If
        Else
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Validation Success\nYou are " & totalAge & "')", True)
        End If
    Else
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Validation Failed\nYou are " & totalAge & "')", True)
    End If
End Sub

Screenshot