So i am making a chat app and i want user to submit username which should not be uppercase and have space, i can accept the name and change it later in onchanged feature but i want user to know it as well
Flutter's TextField or TextFormField has attribute named inputFormatters which takes a list of TextInputFormatter.
an example of TextInputFormatters that is useful in your case.
FilteringTextInputFormatter.allow(RegExp("[a-z]"))
TextField(
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp("[a-z]")),
],
)
you can see TextInputFormatters API docs here : Reference
(before flutter 1.20):
WhitelistingTextInputFormatter(RegExp("[a-z]")),
TextField(
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter(RegExp("[a-z]")),
],
)
you can take those as reference if it's not clear enough : Reference 2, Reference 3.
Also check out this SOF Question: Reference 4
In regards to changing the text, try doing the following:
Let's say 's' is the username:
String s = ""
onChange(val) {
s = val.trim().toLowerCase()
}
If you want to notify the user, perhaps use an alert dialog with some text letting them know the username should not be uppercase and contain no spaces. Regardless, you can't assume the user will conform to what they "should do".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With