2016-06-08 58 views
1

我想對文本框進行驗證,當它爲空時不會提交。它的工作原理是當有空白時它不會提交,但當我還沒有在文本框上輸入任何內容時仍然提交。不要提交空文本框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!"); 
    } 
} 
+0

這是ASP.NET或WinForms的?在這種情況下什麼是「提交」? – Dai

+0

嘗試切換你的IF語句。將檢查空白框放在第一個if,並將檢查匹配放在else中,如果 – Mark

+0

'Click'事件使用按鈕註冊,則不管文本框內容如何,​​都會觸發事件。 –

回答

1

重新形成的條件是這樣

Regex pattern = new Regex("^[a-zA-Z]{0,8}$"); 
if (string.IsNullOrEmpty(textBox1.Text)) 
{ 
    MessageBox.Show("Please type a valid 8 character string on the textbox!"); 
} 
else 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 
{ 
    MessageBox.Show("Please type a valid 8 character string on the textbox!"); 
} 
+0

感謝它的工作 – Dren

1

RegEx語句允許空值。將0更改爲1,{0,8},並刪除捕獲空值的else if塊(值不可能在TextBox上)。

Regex pattern = new Regex("^[a-zA-Z]{1,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 
{ 
    MessageBox.Show("Please type a valid 8 character string on the textbox!"); 
} 
+0

感謝它的工作。 – Dren

0

我會建議先檢查空文本,然後檢查正則表達式,因爲正則表達式允許0到8個字符。

private void button1_Click(object sender, EventArgs e) 
{ 
    if (string.IsNullOrEmpty(textBox1.Text) || textBox1.Text == "") { 
     MessageBox.Show("Please type a valid 8 character string on the textbox!"); 
    } else { 
     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 
     { 
      MessageBox.Show("Please type a valid 8 character string on the textbox!"); 
     } 
    } 
} 

如果它應該是正好是8,你需要在你的正則表達式的{0,8}部分更改爲{8},你可以留下你的代碼的其餘部分究竟如何是。

0

試試這個

else if (string.IsNullOrWhiteSpace(textBox1.Text)) 
{ 
    MessageBox.Show("Please type a valid 8 character string on the textbox!"); 
} 
0

您可以使用TRIM()丟棄空白

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.Trim()) || 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!"); 
    } 

}