Reputation: 6148
How can I limit the TextField
can only input letter and spacing?
I had try:
TextField(
inputFormatters: [
RegexFormatter(regex: '[A-Za-z]+'),
],
),
But it not work.
Upvotes: 1
Views: 110
Reputation: 14775
Refer below code
TextField(
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp("[a-zA-Z0-9 ]"),
),
LengthLimitingTextInputFormatter(10),//for limit of max length
],
),
Upvotes: 1
Reputation: 644
Either of the 2 works.
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(
RegExp(r"[a-zA-Z\s]"),
)
]
or
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(
RegExp(r"[a-zA-Z ]"),
)
]
Upvotes: 2
Reputation: 24912
Try this:
inputFormatters: [ FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]")), ]
For further understanding please refer Restrict Special Character Input Flutter
Upvotes: 1
Reputation: 5986
Try this,
TextField(
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp("[a-zA-Z ]")),
// This will allow only characters and space
],
),
Upvotes: 2