验证c#空字段

本文关键字:字段 验证 | 更新日期: 2023-09-27 18:02:14

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

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

验证c#空字段

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