Reputation: 81
In TextField
after mentioning keyboardType: TextInputType.number
also use is able to type comma(,) and other special characters. So I want to block typing special characters and also allow hyphen(-) only at beginning.
//valid entries
10
10.0
-10
-10.0
.01
//Invalid Entries
10.0.2
10-0
10.0-
I tried to achieve using this
var expression = RegExp('([-]?)([0-9]+)([.]?)([0-9]+)');
TextField(
keyboardType: TextInputType.number,
inputFormatters: [WhitelistingTextInputFormatter(expression)],
controller: _answer,
}
But it didn't worked for me. So i tried to use onChanged: answerOnChangeListener(_answer)
But it is causing lag
answerOnChangeListener(TextEditingController controller) {
int oldLength = controller.text.length;
String newValue = stringMatch(controller.text);
if (oldLength != newValue.length) controller.text = newValue;
}
String stringMatch(String substring) {
String response = expression
.allMatches(substring)
.map<String>((Match match) => match.group(0))
.join();
print("stringMatch : $response");
return response;
}
Upvotes: 8
Views: 4258
Reputation: 1120
You should not use regex only for that but instead, use a custom TextInputFormatter with regex and then validate also. Like this:
inputFormatters:
inputFormatters: [
// Regex to allow only "numbers", "dot" and "minus".
FilteringTextInputFormatter(RegExp("[0-9.-]"), allow: true),
TextInputFormatter.withFunction((TextEditingValue oldValue, TextEditingValue newValue) {
final newValueText = newValue.text;
if (newValueText == "-" || newValueText == "-." || newValueText == ".") {
// Returning new value if text field contains only "." or "-." or ".".
return newValue;
} else if (newValueText.isNotEmpty) {
// If text filed not empty and value updated then trying to parse it as a double.
try {
double.parse(newValueText);
// Here double parsing succeeds so returning that new value.
return newValue;
} catch (e) {
// Here double parsing failed so returning that old value.
return oldValue;
}
} else {
// Returning new value if text field was empty.
return newValue;
}
}),
],
validator:
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter watermark rotation angle';
} else {
try {
double.parse(value);
// Here double parsing succeeds so returning that new value.
return null;
} catch (e) {
// Here double parsing failed so returning that old value.
return 'Please enter watermark rotation angle';
}
}
},
Why validator?
Because when your user starts to type you should accept the value "-" or only "." or only "-." even though they are not valid double values otherwise the user cannot enter these characters or erase the text field sometimes and to allow them we use input formatters. But to verify the user only entered valid values we use a validator that checks if the value entered is valid otherwise shows an error text.
Only want the regex?
But if you just want the regex then for the positive and negative double values only regex is ^(?:[+-])?(?:[0-9]*\.)?[0-9]+$
but I don't know why it doesn't work or how to use it with flutter.
Upvotes: 1
Reputation: 83
"^-?\d"*
Use this regex with
inputFormatter: FilteringTextInputFormatter.allow(new RegExp("^-?\\d*")),
Upvotes: 1