Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow Only lowercase with no space Input Textfield Flutter

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

like image 700
Sanskar Tiwari Avatar asked Nov 06 '25 18:11

Sanskar Tiwari


2 Answers

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

like image 187
Saifallak Avatar answered Nov 09 '25 10:11

Saifallak


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".

like image 29
eandpi Avatar answered Nov 09 '25 10:11

eandpi