在 textField Flutter 中只输入偶数

那么,是否可以在用户只能插入偶数的情况下执行 TextFormField ?现在他们无法输入例如 12,这就是问题所在。

                    child: TextFormField(
                      inputFormatters: [
                        FilteringTextInputFormatter.deny(RegExp("[1,3,5,7,9]"))
                      ],keyboardType: TextInputType.number,),
yamating 回答:在 textField Flutter 中只输入偶数

偶数或奇数可以通过只看最后一位数字来测试,它也需要是偶数或奇数。所以奇数运行的正则表达式可能是:

"$\s*(\d*[13579]\s*,\s*)*\d*[13579]$"

使用正则表达式如下

child: TextFormField(
                      inputFormatters: [
                        FilteringTextInputFormatter.deny(RegExp("$\s*(\d*[13579]\s*,\s*)*\d*[13579]$"))
                      ],keyboardType: TextInputType.number,),

为了在正则表达式下面拒绝偶数使用

"$\s*(\d*[02468]\s*,\s*)*\d*[02468]$"
本文链接:https://www.f2er.com/5964.html

大家都在问