private void txtUsername_Validating(object sender, CancelEventArgs e) { System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9_]"); if (UserID.Text.Length > 0) { if (!rEMail.IsMatch(UserID.Text)) { MessageBox.Show("only letters,numbers and underscore is allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); UserID.SelectAll(); e.Cancel = true; } } }