简体   繁体   English

使用RegEx进行文本框验证

[英]Textbox validation using RegEx

I want to validate textbox within my c# form so I made this method to do check below 我想在我的C#表单中验证textbox ,所以我在下面进行了此方法检查

  1. Only number are allowed. 只允许输入数字。

  2. Only az are allowed. 只允许使用az。

  3. Replacing the more than withe spaces with one space and return the string. 用一个空格替换多于空格并返回字符串。

Problem the only number are allowed always get me the error message even if there is no az. 问题是,即使没有az,也只允许输入数字,总是让我收到错误消息。

Finally any suggestion or improvement to below code ? 最后对以下代码有何建议或改进?

I have made some update now please if their is any suggestion or improvement or any checks usual standard I have missed 如果他们有任何建议或改进或我错过的常规检查标准,请立即进行一些更新

Edit on 20/3/2016 1:12 PM GMT 格林尼治标准时间20/3/2016 1:12 PM编辑

GMT Time now :D 格林尼治标准时间现在 :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(); }
    }

如果我了解您的问题,请使用以下代码匹配文本框中的数字:

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

To test for only numbers in your TextBox try the following regex: 若要仅在文本框中测试数字,请尝试以下正则表达式:

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

I've tested it myself an it works fine for me 我已经自己测试过了,对我来说效果很好

Try: 尝试:

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

This should print: 这应该打印:

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

See Demo here 在这里查看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM