inputFormatters: [
LengthLimitingTextInputFormatter(50),
],
The above code in TextFormField restricted character length not words. I want to set validation that this field only allow to adds Max 50 words in flutter.
You can consider the following ways.
TextFormField(
validator: (text) {
if (text.split(' ').length > max_limit) {
return 'Reached max words';
}
return null;
},
);
TextField(
controller: _text,
decoration: InputDecoration(
labelText: 'Enter 5 words',
errorText: _validate ? 'You have entered more than 5 words' : null,
),
);
On your OnChanged() -> update the _validate variable based on your condition.