不要提交空文本框c#

时间:2016-06-08 03:28:20

标签: 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!");
    }
}

5 个答案:

答案 0 :(得分: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!");
}

答案 1 :(得分:1)

您的RegEx语句允许空值。将0更改为1中的{0,8},然后删除捕获空值的else if块(null上的TextBox值不是&#39} ;甚至可能)。

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

答案 2 :(得分: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},并且可以保留代码的其余部分。

答案 3 :(得分:0)

试试这个

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

答案 4 :(得分:0)

您可以使用Trim()来丢弃空白区域

 <form id="form1" runat="server">
        <div>
           <asp:Table width="100%" CssClass="display responsive nowrap" runat="server" ID="tblCustomers">
               <asp:TableHeaderRow TableSection="TableHeader">
                   <asp:TableHeaderCell>First Name</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Last Name</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Date of Birth</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Age</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Gender</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Address 1</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Address 2</asp:TableHeaderCell>
                   <asp:TableHeaderCell>City</asp:TableHeaderCell>
                   <asp:TableHeaderCell>State</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Zip Code</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Email</asp:TableHeaderCell>
                   <asp:TableHeaderCell>How did you hear about us?</asp:TableHeaderCell>
                   <asp:TableHeaderCell>What you are trying to achieve?</asp:TableHeaderCell>
                   <asp:TableHeaderCell>Plan</asp:TableHeaderCell>
               </asp:TableHeaderRow>
           </asp:Table>
        </div>
    </form>
      </div>
    <script type="text/javascript" >
        $(document).ready(function() {

            $('#tblCustomers').DataTable();

        } );
    </script>
</body>
</html>

}