使用RegEx验证文本框

时间:2016-03-20 10:55:35

标签: c# regex

我想在我的c#表单中验证textbox,所以我让这个方法在下面进行检查

  1. 仅允许编号。

  2. 只允许使用a-z。

  3. 用一个空格替换多个空格并返回该字符串。

  4. 问题是,即使没有a-z,唯一允许的号码总是会收到错误消息。

    最后对代码的任何建议或改进?

    我现在已经做了一些更新,如果他们有任何建议或改进或任何我错过的通常标准检查

    编辑于20/3/2016格林尼治标准时间下午1:12

    GMT时间now :D

                    public void input_validation()
        {
            string num_regex = @"^[0-9]*$";         //only digits allowed in this textbox
            string word_regex = @"[a-zA-Z]+";       //only a-z allowed in this textbox
            string Multi_spaces = @"\s+|\s{2,}";   //more than on white spaces
    
            Regex Nregex = new Regex(num_regex);
            Regex Wregex = new Regex(word_regex);
            Regex Mregex = new Regex(Multi_spaces);
    
            //To check all empty textbox within the groupbox
            foreach (var emptytxtbox in GB_CUST_INFO.Controls.OfType<TextBox>())
            {
                if (string.IsNullOrEmpty(emptytxtbox.Text.Trim()))
                {
                    MessageBox.Show("Missing Information are no allowed\n","Missing Information",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    emptytxtbox.BackColor = Color.Red;
                    return; //to stop the check on first empty textbox 
                }
                else
                {
                    emptytxtbox.BackColor = Color.White; //to rest the color of missed info at pervious check
                }
    
                if (Mregex.IsMatch(emptytxtbox.Text))
                {
                  //just replacing the more than one white spaces with one white space and retrun to its textbox
                    emptytxtbox.Text = Mregex.Replace(emptytxtbox.Text," ");
                }
            }
    
            if (!Nregex.IsMatch(TB_CUST_PHONE1.Text))
            {
                MessageBox.Show("Only Number are allowed for phone number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (!Nregex.IsMatch(TB_CUST_PHONE2.Text))
            {
                MessageBox.Show("Only Number are allowed for mobile number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //else if (Mregex.IsMatch(TB_CUST_NAME.Text))
            //{   //just replacing the more than one white spaces with one white space and retrun to its textbox
            //    TB_CUST_NAME.Text = Mregex.Replace(TB_CUST_NAME.Text, " ");
            //}
            /*else if (Wregex.IsMatch(TB_CUST_NAME.Text))
            {
                MessageBox.Show("Only a-z are allowed");
            }*/
            else { cust_data_insert(); }
        }
    

3 个答案:

答案 0 :(得分:0)

如果我了解您的问题,要匹配文本框中的数字,您应该使用:

string num_regex = @"^[0-9]*$";   

答案 1 :(得分:0)

要仅测试TextBox中的数字,请尝试以下正则表达式:

string num_only_regex = @"[0-9]+$";

我自己测试过,它对我来说很好用

答案 2 :(得分:0)

尝试:

var text = "abc12-   443";
var sanitizedText = string.Empty;

var result = Regex.Match(text, @"^[a-z0-9\s]+$",
             RegexOptions.IgnoreCase);

if (result.Success)
{
    sanitizedText = Regex.Replace(result.Value, "[ ]+", " ");
    Console.WriteLine(sanitizedText);
}
else
{
    Console.WriteLine("Invalid input: ({0})", text);
}

这应该打印:

abc12 443   #for abc12   443
Invalid input: (abc12-   443)     #for abc12- 443

请参阅Demo此处