我想对文本框进行验证,当它为空时不会提交。它的工作原理是当有空白时它不会提交,但当我还没有在文本框上输入任何内容时仍然提交。不要提交空文本框c#
下面是C#代码
private void button1_Click(object sender, EventArgs e)
{
Regex pattern = new Regex("^[a-zA-Z]{0,8}$");
if (pattern.IsMatch(textBox1.Text))
{
button1.Text = "Submitted";
button1.Enabled = false;
string name = textBox1.Text;
int ver = 2013;
MessageBox.Show("Hello " + name + "! Welcome to visual c# " + ver.ToString(), "Greeting");
}
else if (string.IsNullOrEmpty(textBox1.Text) || textBox1.Text == "")
{
MessageBox.Show("Please type a valid 8 character string on the textbox!");
}
else
{
MessageBox.Show("Please type a valid 8 character string on the textbox!");
}
}
这是ASP.NET或WinForms的?在这种情况下什么是“提交”? – Dai
尝试切换你的IF语句。将检查空白框放在第一个if,并将检查匹配放在else中,如果 – Mark
'Click'事件使用按钮注册,则不管文本框内容如何,都会触发事件。 –