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