flutter 扑动|如何为有效的双精度创建RegExp?

njthzxwz  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(88)

FilteringTextInputFormatter -如何创建RegExp以防止用户键入多个句点、键入逗号、在零后面键入除句点以外的数字等。

osh3o9ms

osh3o9ms1#

试试这个:

TextField(
                      decoration: InputDecoration(
                        labelText: 'Enter a number:',
                      ),
                      keyboardType:
                          TextInputType.numberWithOptions(decimal: true),
                      inputFormatters: [
                        FilteringTextInputFormatter(
                            RegExp(r'^\d*\.?\d{0,1}$'),
                            allow: true),
                      ],
                    ),
zynd9foi

zynd9foi2#

解决了。

List<TextInputFormatter>? inputFormatters = [
  FilteringTextInputFormatter.allow(
    RegExp(r'(^\d*\.?\d*)'),
  ),

  FilteringTextInputFormatter.deny(
    RegExp(r'\.\.+'), 
    replacementString: '.',
  ),

  FilteringTextInputFormatter.deny(
    RegExp(r'^\.'), 
    replacementString: '0.',
  ),

  FilteringTextInputFormatter.deny(
    RegExp(r'\d+-'), 
  ),

  FilteringTextInputFormatter.deny(
    RegExp(r'\.-'),
    replacementString: '.',
  ),

  FilteringTextInputFormatter.deny(
    RegExp(r'-\.+'),
  ),

  FilteringTextInputFormatter.deny(
    RegExp(r'^0\d+'),
    replacementString: '0.',
  ),
];

相关问题