How to prevent entering numbers in Flutter TextFormField? - flutter

I am developing an application with Flutter. I put TextFormField. I want to prevent entering numbers in this TextFormField. How can I do that?

You can use the validator argument to create a callback if the user input a number.
You can also add the keyboardType argument to limit the user to only text with the value TextInputType.text

Try this:
TextFormField(
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.deny(RegExp('[0-9]')),
],
),

inputFormatters: [
FilteringTextInputFormatter.deny(RegExp("[0-9]")),
],
You need to add this

Related

How can I change the text when its length 14 characters with TextInputFormat?

well, I have a textFormField. This TextFormField will use to take user phone number. What I wanna thing is when the phone number length is 14 characters, all texts which on seems on the TextFormField would "xxx xxx".
How can I doing this?
You can use mask_text_input_formatter
TextFormField(
inputFormatters: [
ExampleMask(
formatter: MaskTextInputFormatter(mask: "+# (###) ###-##-##"),
textInputType: TextInputType.phone
),
],

KeyboardType namePhonePad in Flutter

In iOS, we have keyboard type namePhonePad and the Normal behavior of this keyboard type is as shown in the below snapshot.
and the output is, (This is what I want in Flutter)
This is the output of iOS's namePhonePad keyboard type.
I want the same behavior for the flutter keyboard type.
I have reviewed and there is no namePhonePad keyboard type in a flutter. We have separate types like phone, number, name, etc,
but not namePhonePad
Flutter's output is,
You should try the package keyboard_actions
You can use the keyboardType of TextFormField widget with TextInputType.number and FilteringTextInputFormatter.digitsOnly... example:
Expanded(
child: TextFormField(
controller: txt_identidad,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
),
),

Flutter: detect if a string can be converted to double

I can do something like this:
double.parse(myString)
That's fine when I have "1.1" but if I have "1.1.1" it fails with Invalid double. Could I detect this somehow in advance?
I'd need this for input validations
Working with
double.tryParse(myString)
As written above, use double.tryParse() or wrap double.parse() with try catch
An alternative, if you need to do input validation could be filter away "bad" input already when the user inputs the number.
You could change keyboard type and use input formatters on a TextField / TextFormFiel
TextField(
decoration: new InputDecoration(labelText: "Enter your number"),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter> [
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
),
You could write something like this
if(!myString.contains(<someregex>)) {
double.parse(myString);
}
Where the regex is validating that the string is a valid double, the value of doing this over using tryParse is that you can include your business rule validation alongside your datatype validation.

How to display in TextField only double value

I've got Text field which updateds a value variable to double format in onChange... I want the TextField only display the updated the value variable.. I don't want the textfield to display any string value or values which are not in double format by pressing wrong keyboard key. Even with numeric keyboard with decimal enabled we can press decimal key multiple times which I don't want to display in the TextField.
TextFormField(
decoration: InputDecoration(labelText: 'Value'),
onChanged: (s) {
if (double.tryParse(s) != null)
setState(() {
value = double.parse(s);
});
},
)
How can I display only parsed value in TextField?
Please add below attribute to TextFormField Widget
TextFormField(
keyboardType:TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}'))
],
);
#user8773560 was correct however RegExp for decimal number was wrong and not working so the correct answer is
inputFormatters: [
WhitelistingTextInputFormatter(RegExp(r'(^\d*\.?\d*)'))
]
Also answered in my other question Regex for double numbers in dart
Answer based on #delmin's answer, but allows negative numbers as well.
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'(^-?\d*\.?\d*)'))
]
Hello you can not use onChange for this purpose because its called only once TextField has changed.
You should try this.
TextFormField(
inputFormatters: <TextInputFormatter>[
LengthLimitingTextInputFormatter(12), //max length of 12 characters
WhitelistingTextInputFormatter.digitsOnly,//Only numbers
BlacklistingTextInputFormatter.singleLineFormatter,//No line break
WhitelistingTextInputFormatter(RegExp("[0-9]+.[0-9]")) //only double values
],
)
Lear more here or Official doc

Restrict Special Character Input Flutter

I have a TextField that will only take numbers as an input.
The Keyboard that appears allows for the input of the the following characters: "-(hyphen) ,(comma) .(period) and space". How can I prevent the user from entering all except the period.
child: new TextField(
controller: _controller,
maxLengthEnforced: true,
keyboardType: TextInputType.number,
maxLength: 4, //9999 upper limit
), //TextField
I've tried to use RegExp to take the _controller text, remove the characters and place it back into to field without any luck.
...//Add Listener to _controller
_controller.addListener(restrictCharacters);
...
//Listener
void restrictCharacters(){
RegExp regexp = new RegExp(
r"^|\-|\,|\ ",
caseSensitive: false,
multiLine: false,);
String text = _controller.text;
String chng = text.replaceaLL(regExp,"");
_controller.text = chng;
}
When applied the cursor moves to beginning and the field keeps the - (hyphen) for example.
In your TextInputField(), use a FilteringTextInputFormatter like this:
TextField(
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp("[0-9a-zA-Z]")),
], // Only numbers can be entered
),
Each character typed will be allowed only if it matches the RegExp. To include a space, use the regular expression "[0-9a-zA-Z ]" instead. Period means "any character" in regular expressions unless you escape them, like "[0-9\.a-zA-Z]".
.deny can be used in place of .allow to proved a blacklist instead of a whitelist.
WhitelistingTextInputFormatter is deprecated. You should use FilteringTextInputFormatter.allow:
inputFormatters: [ FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]")), ]
or FilteringTextInputFormatter.deny instead of BlacklistingTextInputFormatter
Add a BlacklistingTextInputFormatter to your TextField.
inputFormatters: [
new BlacklistingTextInputFormatter(new RegExp('[\\.]')),
],
removes just . If you wanted to disallow, say, . and , change it to
inputFormatters: [
new BlacklistingTextInputFormatter(new RegExp('[\\.|\\,]')),
],
Add this to your TextField for letters and numbers
inputFormatters: [new WhitelistingTextInputFormatter(RegExp("[a-zA-Z0-9]")),],
If you want double to be added,you can also use the format like this:
inputFormatters: [FilteringTextInputFormatter.allow(RegExp("[0-9\.]")),],
Be sure to include the backslash, as a period on its own means "any character" (allowing everything).
Dart 2.13+, Flutter 1.27+
inputFormatters:[FilteringTextInputFormatter.allow(RegExp('[a-zA-Z0-9]'))]
For those who need to work with money format in the text fields:
To use only: , (comma) and . (period)
and block the symbol: - (hyphen, minus or dash)
as well as the: ⌴ (blank space)
In your TextField, just set the following code:
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [BlacklistingTextInputFormatter(new RegExp('[ -]'))],
The simbols hyphen and space will still appear in the keyboard, but will become blocked.
inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.allow(RegExp("[0-9a-zA-Z]")),],
if you want to include English and Devanagari character also in your name
then follow this regex pattern
I have created this pattern for English letter and Devanagari letters with allow space
inputFormatters: [ WhitelistingTextInputFormatter(RegExp("[a-zA-Z \u0900-\u097F]")),
LengthLimitingTextInputFormatter(30),
],
You can deny special characters with escape sequences
or get any escape sequence from google which you want
Example:
inputFormatters: [
FilteringTextInputFormatter.deny(
RegExp("[a-zA-Z0-9\u0020-\u007E-\u0024-\u00A9]"),
),
],
None of the above answers worked for me, This is how to use FilteringTextInputFormatter inside TextFormField:
TextFormField(
inputFormatters: [
FilteringTextInputFormatter(
RegExp("[a-zA-Z]"),
allow: true),
],)
Set allow to false if you want to deny the RegExp.
Add this to your TextField for letters and numbers
inputFormatters: [WhitelistingTextInputFormatter(RegExp("[a-zA-Z0-9]")),],