public bool IsValidYear(String year) { Regex objyr = new Regex("^([0-9]{4})-([0-9]{2})"); return objyr.IsMatch(year); } public void checkyr() { if (IsValidYear(txtassyr.Text)) { string[] strarry = txtassyr.Text.Split(' '); string temp; int i, j; foreach (string s in strarry) { string[] str = s.Split('-'); temp = str[0].Substring(2); i = Convert.ToInt32(temp); j = Convert.ToInt32("1"); if (str[1].StartsWith("0")) { str[1] = str[1].Replace("0", ""); } if (str[1].Equals(Convert.ToString(i + j))) { // } else { //MessageBox.Show(Convert.ToString(i + j)); MessageBox.Show("Please enter Ass-Year in following format 2008-09"); txtassyr.Text = ""; txtassyr.Focus(); } } } else { MessageBox.Show("Please enter Ass-Year in following format 2008-09"); txtassyr.Text = ""; txtassyr.Focus(); } }