Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

private void btnsave_Click(object sender, EventArgs e)
{
   if (txtfname.Text == "" )
   {
      MessageBox.Show("Please enter your First Name");
      txtfname.Focus();
   }
}

I want to validate a textbox such that a person cannot leave it blank nor can they add numbers. Do I have to write an "else if" statement or is there any operator that I can use to put it all in the same if statement.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
230 views
Welcome To Ask or Share your Answers For Others

1 Answer

The operator you can use is ||

string fName = txtfname.Text;
if (string.IsNullOrWhiteSpace(fName) || fName.Any(Char.IsDigit))
{
    MessageBox.Show("Please enter your First Name without digits");
    txtfname.Select();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...