Reputation: 636
I am trying to use a regular expression validation to check for only decimal values or signed numeric values in a Flutter App.
Valid inputs are like
I have used WhitelistingTextInputFormatter
to restrict the user inputs. Please refer the code below:
TextFormField(
key: AppKeys.emailField,
keyboardType: TextInputType.emailAddress,
controller: controller.emailTextController,
inputFormatters: [
WhitelistingTextInputFormatter(RegExp(r'[0-9-]\d*(\.\d+)?')),
],
maxLength: 100,
decoration: InputDecoration(
labelText: Strings.emailPrompt,
counterText: '',
prefixIcon: Icon(Icons.email),
),
//validator: Validator.validateEmail,
onSaved: (String val) {
//_email = val;
},
);
But this seems to have not working. Though it's working fine for only numbers it's not accepting dot character. Regex should also accept one minus character that to be at the starting and one dot character that to be in the middle.
Any help is highly appreciate. Thanks in Advance.
Upvotes: 2
Views: 3338
Reputation: 71
This will work fine. Textfield doesn't become blank, if invalid character is entered.
WhitelistingTextInputFormatter(RegExp(r'(^\-?\d*\.?\d*)')),
Upvotes: 7
Reputation: 107121
Check with the following input formatter:
WhitelistingTextInputFormatter(RegExp(r'(^\-?\d*\.?\d*)$')),
Upvotes: 0