【问题标题】:Mask in Multiline TextBox多行文本框中的蒙版
【发布时间】:2010-11-29 08:34:59
【问题描述】:

我想实现一个文本框,用户只能在这样的模式中插入文本:

dddddddddd, dddddddddd, dddddddddd, ...

其中 d 是一个数字。如果用户在一行中以少于 10 位的数字离开控件,验证会失败,并且他不能在一行中写入超过 10 位的数字,那么可接受的应该只有逗号“,”。

感谢您的帮助

【问题讨论】:

    标签: c# regex winforms


    【解决方案1】:
     Match m = Regex.Match(textBox.Text, @"^\d{10},$", RegexOptions.Multiline);
    

    没试过,应该可以的。请查看here 和here 了解更多信息。

    【讨论】:

    • 在 TextBox 控件中我应该在哪里设置这个?
    • 您应该有某种按钮或其他动作驱动的组件来启动验证。例如,一个名为“继续”或“下一步”或类似名称的按钮。这实际上取决于您如何设置流程。
    • 如果至少有一行(但不一定是所有行)遵循此模式,这将返回 True。
    【解决方案2】:

    我建议使用正则表达式

    \A(?:\s*\d{10},)*\s*\d{10}\s*\Z
    

    说明:

    \A        # start of the string
    (?:       # match the following zero or more times:
     \s*      # optional whitespace, including newlines
     \d{10},  # 10 digits, followed by a comma
    )*        # end of repeated group
    \s*       # match optional whitespace
    \d{10}    # match 10 digits (this time no comma)
    \s*       # optional whitespace
    \Z        # end of string
    

    在 C# 中,这看起来像

    validInput = Regex.IsMatch(subjectString, @"\A(?:\s*\d{10},)*\s*\d{10}\s*\Z");
    

    请注意,您需要使用逐字字符串 (@"...") 或将正则表达式中的所有反斜杠加倍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多