How to validate age 18 or not?

Posted by Shubham under C# category on | Points: 40 | Views : 1415
set a textbox(txtDob)on page and set the calender on textbox click event and select the date from calender...

protected void btn_Click(object sender, EventArgs e)
DateTime dtStart = DateTime.Parse(txtDoB.Text);
TimeSpan sp = DateTime.Now - dtStart;
int year =18\\\\\\ pass the no of year of no you want to validate
int days = 365;
int total = year * days;

if (sp.Days < total)

txtDoB.Text = "Not Valid";
lblMess.Text = "You can not be under 18 years";
lblMess.CssClass = "Errr";

Comments or Responses

Login to post response