验证C#空字段

验证C#空字段,c#,validation,C#,Validation,我想在用户签署注册表时检查一些项目(唯一用户名、输入的两个类似密码、电子邮件的有效性,而不是空字段),这是我的代码。但不适用于空白字段。问题出在哪里 private void button1_Click(object sender, EventArgs e) { try { //Check Username: if username is unavailable, goto catch and continue regist

我想在用户签署注册表时检查一些项目(唯一用户名、输入的两个类似密码、电子邮件的有效性,而不是空字段),这是我的代码。但不适用于空白字段。问题出在哪里

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            //Check Username: if username is unavailable, goto catch and continue register
            var q = (from c in Session.DB.Guests
                     where c.UserName == textBox4.Text
                     select c).Single();
            MessageBox.Show("UserName is Not Available");
        }
        catch
        {
            try
            { // Here My code For Blank Fields:
              if (!(textBox1.Text == null && textBox2.Text == null && textBox3.Text == null && 
               textBox4.Text == null && textBox5.Text == null && textBox6.Text == null))  

                     if (!(textBox5.Text.Contains(textBox4.Text) || textBox5.Text != textBox6.Text)) //Check Password
                         if (textBox3.Text.Contains("@") && textBox3.Text.Contains("."))
                        {
                            Controller.UserController.RegisterUser(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text
                                , textBox5.Text);
                            MessageBox.Show("Register!");
                        }

                    else
                        MessageBox.Show("Incorrect email address");
                else
                    MessageBox.Show("Password Not match or contains username");
                else MessageBox.Show("Empty Fields! All Fields Required!");
            }
            catch
            {
                MessageBox.Show("Error");
            }
        }}

我认为你的代码中缺少了一些逻辑。此外,您应该尽量避免以这种方式使用try-catch块,并且应该尝试为控件指定有意义的名称,这将使代码更具可读性。试着这样做:

// textBox4 should be txtUserName for example, it's more intuitive
var q = (from c in Session.DB.Guests
         where c.UserName == textBox4.Text
         select c).SingleOrDefault();

if (q == null)
{
    MessageBox.Show("UserName is Not Available");
}
else
{
    if (textBox1.Text == null || textBox2.Text == null || textBox3.Text == null ||
        textBox4.Text == null || textBox5.Text == null || textBox6.Text == null)
    {
        MessageBox.Show("Empty Fields! All Fields Required!");
    }
    else
    {
        // textBox5 should be txtPassword and textBox6 should be txtConfirmPassword
        if (textBox5.Text.Contains(textBox4.Text) || textBox5.Text != textBox6.Text)
        {
            MessageBox.Show("Password Not match or contains username");
        }
        else
        {
            // textBox3 should be txtEmail, also you should try to find some existing Regex to validate your email
            if (!textBox3.Text.Contains("@") || !textBox3.Text.Contains("."))
            {
                MessageBox.Show("Incorrect email address");
            }
            else
            {
                Controller.UserController.RegisterUser(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text);
                MessageBox.Show("Register!");
            }
        }
    }
}

为什么主要代码写在catch块中?当出现异常(错误)时,捕捉块将进入画面。问题的标题也有误导性。请更正。@Nimesh我在每种情况下都使用try-catch来表示错误,它是假的吗?