skip to Main Content

FilteringTextInputFormatter – How to create RegExp to prevent the user from typing more than one period, from typing a comma, from typing numbers after zero except period, etc.

2

Answers


  1. try this:

    TextField(
                          decoration: InputDecoration(
                            labelText: 'Enter a number:',
                          ),
                          keyboardType:
                              TextInputType.numberWithOptions(decimal: true),
                          inputFormatters: [
                            FilteringTextInputFormatter(
                                RegExp(r'^d*.?d{0,1}$'),
                                allow: true),
                          ],
                        ),
    
    Login or Signup to reply.
  2. Resolved.

    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'^0d+'),
        replacementString: '0.',
      ),
    ];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search