在一个字段C#上执行多个验证

时间:2013-12-30 20:20:56

标签: c# validation textbox

我必须验证文本框以检查它们是否是数字以及它们是否在数据库中。唯一的问题是我似乎只能检查它的有效性或它们是否为数字。怎样才能改变它以获得两种验证?

验证确保文本框中有某些内容:

if (string.IsNullOrEmpty(employeeIDTextBox.Text) && (string.IsNullOrEmpty(JobIDTextBox.Text)))

然后查看该值是否为数字,如果是,则检查该人是否存在,然后设置值

else if (string.IsNullOrEmpty(JobIDTextBox.Text))
     {
        if (!Int32.TryParse(JobIDTextBox.Text, out number1))
        {
           using (dbConn)
           {
              ReportGrid newGrid = new ReportGrid();

              if (newGrid.isValidEmp(Int32TryParseSafe(employeeIDTextBox.Text)))
              {
                  newGrid.startDate = startingdateTimePicker.Value;
                  newGrid.endDate = endingdateTimePicker.Value;

                  newGrid.EmployeeID = Int32TryParseSafe(employeeIDTextBox.Text);
                  newGrid.JobID = Int32TryParseSafe(JobIDTextBox.Text);

                  newGrid.ShowDialog();
               }
               else
               {
                   MessageBox.Show("No ID found for the employee.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

然后它对其他文本框执行相同的操作

else if (string.IsNullOrEmpty(employeeIDTextBox.Text))
      {
          if (!Int32.TryParse(emplyeeIDLabel.Text, out number2))
          {
             using (dbConn)
             {
                 ReportGrid newGrid = new ReportGrid();

                 if (newGrid.isValidJob(Int32TryParseSafe(JobIDTextBox.Text)))
                 {
                     newGrid.startDate = startingdateTimePicker.Value;
                     newGrid.endDate = endingdateTimePicker.Value;

                     newGrid.EmployeeID = Int32TryParseSafe(employeeIDTextBox.Text);
                     newGrid.JobID = Int32TryParseSafe(JobIDTextBox.Text);

                     newGrid.ShowDialog();
                   }

                   else
                   {
                      MessageBox.Show("No ID found for that job.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                   }
                }
             }
             else
                MessageBox.Show("Must be a number.");
           }

这是整个代码

try
{

    if (string.IsNullOrEmpty(employeeIDTextBox.Text) && (string.IsNullOrEmpty(JobIDTextBox.Text)))
    {
        MessageBox.Show("Please enter a EmployeeID or JobID.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
     else if (string.IsNullOrEmpty(JobIDTextBox.Text))
     {
        if (!Int32.TryParse(JobIDTextBox.Text, out number1))
        {
           using (dbConn)
           {
              ReportGrid newGrid = new ReportGrid();

              if (newGrid.isValidEmp(Int32TryParseSafe(employeeIDTextBox.Text)))
              {
                  newGrid.startDate = startingdateTimePicker.Value;
                  newGrid.endDate = endingdateTimePicker.Value;

                  newGrid.EmployeeID = Int32TryParseSafe(employeeIDTextBox.Text);
                  newGrid.JobID = Int32TryParseSafe(JobIDTextBox.Text);

                  newGrid.ShowDialog();
               }
               else
               {
                   MessageBox.Show("No ID found for the employee.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

             }
         }
         else
            MessageBox.Show("Must be a number.");

         if (startingdateTimePicker.Value > endingdateTimePicker.Value)
         {
             MessageBox.Show("Starting data can not be after than ending date.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
      }
      else if (string.IsNullOrEmpty(employeeIDTextBox.Text))
      {
          if (!Int32.TryParse(emplyeeIDLabel.Text, out number2))
          {
             using (dbConn)
             {
                 ReportGrid newGrid = new ReportGrid();

                 if (newGrid.isValidJob(Int32TryParseSafe(JobIDTextBox.Text)))
                 {
                     newGrid.startDate = startingdateTimePicker.Value;
                     newGrid.endDate = endingdateTimePicker.Value;

                     newGrid.EmployeeID = Int32TryParseSafe(employeeIDTextBox.Text);
                     newGrid.JobID = Int32TryParseSafe(JobIDTextBox.Text);

                     newGrid.ShowDialog();
                   }

                   else
                   {
                      MessageBox.Show("No ID found for that job.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                   }
                }
             }
             else
                MessageBox.Show("Must be a number.");
           }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

1 个答案:

答案 0 :(得分:1)

我可能会弄错,但这条线可能是问题吗?

if (!Int32.TryParse(emplyeeIDLabel.Text, out number2))

感叹号会反转bool,因此如果文本成功解析为数字,则TryParse函数返回true,但通过使用感叹号,if语句解析为false。因此,您将代码发送到else语句,该语句表明它不是数字。

另外,尝试使用“Return”来避免嵌套ifs。

if (string.IsNullOrEmpty(employeeIDTextBox.Text) && (string.IsNullOrEmpty(JobIDTextBox.Text)))
{
    MessageBox.Show("Please enter a EmployeeID or JobID.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    Return;
 }

此时不需要else,因为如果if语句解析为true,您将从该方法返回。

嵌套if语句经常是必需的,但是当它们能够使代码更清晰以便于维护时,应该避免使用它们。