TextFormField(
inputFormatters: [
FilteringTextInputFormatter.deny(RegExp('[a-zA-Z]')),
FilteringTextInputFormatter.allow(RegExp('[0-9]')),
],
maxLength: 2,
controller: controller,
),
),
I want to prevent the user from entering a number greater than 12 in the TextFormField. It can only write numbers between 1-12(including 12).
5
Answers
You could try with this custom formatter:
You have to define a custom formatter, check this answer
You can also achieve like this, not sure it is the best way but the idea it to use a dialog to prevent user and force the previous value if it does not fit requirements you want.
try This one its one help to you
You can still use regex like this one
FilteringTextInputFormatter.allow(RegExp(r'^([1][0-2]?|[1-9])$'))
But it is better to use onChange in text field and create function to check if value is > 12
You can use TextFormField as
And TextEditing Controller