使用正则表达式验证文本框,仅包含只能包含一定量值的数值

时间:2016-03-05 21:32:22

标签: c# sql regex validation

我正在尝试验证联系号码文本框只能容纳11位数字。我已经验证了该程序,以说明用户何时没有输入任何值或者用户未使用非数字值。我目前正在尝试使用正则表达式来验证它只保存数值并且长度为11位但我一直遇到逻辑问题,我已经通过逻辑反复尝试使用if循环和其他函数方式,但我真的是正则表达式的新手,我确信问题就在于此。感谢。

以下是正则表达式代码:

Regex checkContactNum = new Regex(@"[0-9]\\d{11}");

以下是整个方法代码:

   private void addEmployee_Click(object sender, EventArgs e)
    {
        try
        {
            Regex checkContactNum = new Regex(@"[0-9]\\d{11}");

             if (string.IsNullOrEmpty(addFirstName.Text) ||
                            string.IsNullOrEmpty(addLastName.Text) || 
               string.IsNullOrEmpty(addRole.Text) || string.IsNullOrEmpty(addContactNum.Text))
            {
                MessageBox.Show("Enter a value");
                if (string.IsNullOrEmpty(addFirstName.Text))
                {
                    errorFirstName.Visible = true;
                    addFirstName.Focus();
                }
                if (string.IsNullOrEmpty(addLastName.Text))
                {
                    errorLastName.Visible = true;
                    addLastName.TabIndex = 0;
                }

                if (string.IsNullOrEmpty(addRole.Text))
                {
                    errorRole.Visible = true;
                    addRole.TabIndex = 1;
                }
                if (string.IsNullOrEmpty(addContactNum.Text))
                {
                    errorContactNum.Visible = true;
                    addContactNum.TabIndex = 2;
                }
            }
            if (!int.TryParse(addContactNum.Text, out parsedValue) && addContactNum.Text != "")//checks if there is any numeric values
            {                                                           //cant have if null or empty due to message box, need a way to exclude
                MessageBox.Show("This is a number only field");
                errorContactNum.Visible = true;
            }
            if (checkContactNum.IsMatch(addContactNum.Text) == false)
            {
                MessageBox.Show("This needs to be 11 digits long.");
                errorContactNum.Visible = true;
            }
            if (addFirstName.Text != "" && addLastName.Text != "" && addRole.Text != "" && addContactNum.Text != "" && !int.TryParse(addContactNum.Text, out parsedValue) && checkContactNum.IsMatch(addContactNum.Text) == true)
            {
                    errorFirstName.Visible = false;
                    errorLastName.Visible = false;
                    errorRole.Visible = false;
                    errorContactNum.Visible = false;

                    string addEmployee = "INSERT INTO Employee (FirstName, LastName, Role, DateOfHire)" +
                            "VALUES (@FirstName, @LastName, @Role, @DateOfHire)";


                    OleDbCommand cmd = new OleDbCommand(addEmployee, conn);
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = addFirstName.Text;
                    cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = addLastName.Text;
                    cmd.Parameters.Add("@Role", OleDbType.VarChar).Value = addRole.Text;
                    cmd.Parameters.Add("@DateOfHire", OleDbType.VarChar).Value = addDateOfHire.Text;

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();

                    addFirstName.Text = String.Empty;
                    addLastName.Text = String.Empty;
                    addContactNum.Text = String.Empty;
                    addRole.SelectedIndex = 0;

                    addFirstName.TabIndex = 0;
                    addRole.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            }
        }
        catch
        {
            MessageBox.Show("Report Bug to maker.");
        }

图片1: This is showing my program refusing to take 11 digits.

1 个答案:

答案 0 :(得分:1)

您可以使用:

@"^\d{11}$"

@"^[0-9]{11}$"

private void TextBox1_Changed(object sender, EventArgs e){
    if(Regex.IsMatch(TextBox1.Text, @"^\d{11}$"){
        //  matches
    } 
    else
    {
        // doesn't mstch
    }
}

对于完整的验证我建议做这样的事情:

string err = "";
if(TxtFirstname.Text.Trim()=="") err+= "....\r\n"  ;
if(TxtLastname.Text.Trim()=="") err+= "....\r\n" ; 
if(!Regex.IsMatch(TextBox1.Text, @"^\d{11}$")) err+= "....\r\n" ; 
.
.
.
if(err=="")
   //Save It
else
   MessageBox.Show(err);