Reputation: 1115
I am using a TextFormField to get a number as input so i used this snippet
keyboardType: TextInputType.number,
This is my TextFormField Widget code
TextFormField(
keyboardType: TextInputType.number,
style: textstyle,
controller: principal,
validator: (String value) {
if (value.isEmpty) {
return "Please enter principal amount e.g 3245";
}
},
decoration: InputDecoration(
labelText: "Principal",
labelStyle: textstyle,
hintText: "Rupees",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
)
But now i can paste some text in the same field as shown here
So now how can i format my TextFormfield to restrict to type only number & to restrict pasting texts?
Upvotes: 2
Views: 5435
Reputation: 6245
UPDATE
Deprecated API removed after v2.5
Old answer
You can try to use WhitelistingTextInputFormatter
like
inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
See official docs
Upvotes: 3
Reputation: 82
The accepted answer is now deprecated as of Flutter version 1.20.0-1.0.pre. Now, use inputFormatters: [FilteringTextInputFormatter.digitsOnly],
instead (see documentation).
In addition, if you want finer control onto what to input using RegEx, use FilteringTextInputFormatter.allow(RegExp(r'your regex here'))
.
Upvotes: 4