flutter Textfield Inputformatters不会与我的自定义发行

发布于 2025-02-02 03:38:18 字数 563 浏览 1 评论 0原文

我想将这种输入允许到我的文本字段:

123
*123#
*123*4#

因此,我创建和测试了Regexr网站此REGEX:

\ ** \ d+\ ** \ ** \ d+d+\#?

但是,当我尝试键入时,在使用的文本字段

代码中键入什么:

     ...

     keyboardType = TextInputType.phone;

     // to allow digits with asterik and hash
     final regex = RegExp(r'\**\d+\**\d+\#?');

     inputFormatters = [FilteringTextInputFormatter.allow(regex)];

     return TextField(
      ...
      keyboardType: keyboardType,
      inputFormatters: inputFormatters,
     );

I want to allow this kind of input to my text field:

123
*123#
*123*4#

so I created and tested RegExr website this regex:

\**\d+\**\d+\#?

but when i try to type nothing is typed in the text field

code of using:

     ...

     keyboardType = TextInputType.phone;

     // to allow digits with asterik and hash
     final regex = RegExp(r'\**\d+\**\d+\#?');

     inputFormatters = [FilteringTextInputFormatter.allow(regex)];

     return TextField(
      ...
      keyboardType: keyboardType,
      inputFormatters: inputFormatters,
     );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2025-02-09 03:38:18

如果您还想将变体与单个数字匹配,例如*1#您可以使用负面的lookahead不包括不存在的内容:

^(?!.*\*[*#]|\d*#$)[*\d]*#?$

说明

  • ^字符串的开始
  • (?!负面lookahead,断言右边是什么
    • 。*\*[*#]匹配***#
    • |
    • \ d*#$在字符串结束时匹配可选数字和
  • ) close lookahead
  • [*\ d]*#?匹配可选*字符或数字和可选
  • $字符串的结尾

请参见a Regex Demo

If you also want to match a variation with a single digit like *1# you might use a negative lookahead excluding what can not be present:

^(?!.*\*[*#]|\d*#$)[*\d]*#?$

Explanation

  • ^ Start of string
  • (?! Negative lookahead, assert what to the right is not
    • .*\*[*#] Match either ** or *#
    • | Or
    • \d*#$ Match optional digits and # at the end of the string
  • ) Close lookahead
  • [*\d]*#? Match optional * chars or digits and optional #
  • $ End of string

See a regex demo.

哑剧 2025-02-09 03:38:18

您可以使用

^\*?(?:\d+\*?(?:\d+#?)?)?$

regex demo

详细信息

  • ^ - String的开始
  • \*? - 可选* char <
  • /codh (?:\ code) d+\*?(?:\ d+#?)?)? -
    • \ d+ - 一个或多个数字
    • \*? - 可选*
  • (?:\ d+#?)? - 一个或多个数字的可选顺序和一个可选的 char
  • $ - 字符串结尾。

You can use

^\*?(?:\d+\*?(?:\d+#?)?)?$

See the regex demo.

Details:

  • ^ - start of string
  • \*? - an optional * char
  • (?:\d+\*?(?:\d+#?)?)? - an optional sequence of
    • \d+ - one or more digits
    • \*? - an optional *
  • (?:\d+#?)? - an optional sequence of one or more digits and an optional # char
  • $ - end of string.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文