验证C#Null Fields

时间:2015-07-12 12:19:20

标签: c# validation

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

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

1 个答案:

答案 0 :(得分:0)

我认为你错过了代码中的一些逻辑。此外,您应该尽量避免以这种方式使用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!");
            }
        }
    }
}