Validation in Windows Form

Sabarimahesh
Posted by Sabarimahesh under C# category on | Points: 40 | Views : 1753
private void txtDateOfBirth_Validating(object sender, CancelEventArgs e)
{
DateTime dob;
// DOB is required and must be a valid date
if (!DateTime.TryParse(txtDateOfBirth.Text, out dob))
{
// errorProvider1.SetError(txtDateOfBirth, "Must be a date/time value");
MessageBox.Show("Date Formate Wrong");
e.Cancel = true;
return;
}
// Must be 18 or older
if (dob > DateTime.Now.AddYears(-18))
{
//errorProvider1.SetError(txtDateOfBirth, "Must be 18 to 75");
MessageBox.Show("UR Are Not Eligible(AGE Limit: 18 to 75)");
e.Cancel = true;
return;
}
if ( (dob < DateTime.Now.AddYears(-75)))
{
//errorProvider1.SetError(txtDateOfBirth, "Must be 18 to 75");
MessageBox.Show("Ur age Limit Exceeds");
e.Cancel = true;
return;
}
// DOB is valid
// errorProvider1.SetError(txtDateOfBirth, "");

}

Comments or Responses

Login to post response