How to accept only numbers in TextFormFields? - flutter

I want to accept only numbers in TextFormFields. I have designed a TextFormField for accepting phone numbers. I have changed the keyboard type.
keyboardType: TextInputType.phone,
But, it can also accept special symbols or can paste other inputs also.
Keyboard screenshots

Try below code, refer FilteringTextInputFormatter
import below library
import 'package:flutter/services.dart';
Your Widget:
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
//you can used below formater also
/* inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp("[0-9]"),
),
],*/
),

In Your TextFormField add:
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp("[0-9]")),
],
You can also modify RegExp for supporting any kind of input characters.

Try used input formatter
import 'package:flutter/src/services/text_formatter.dart';
TextFormField(
keyboardType: TextInputType.phone,
inputFormatters:
[
FilteringTextInputFormatter.digitsOnly
]
)

You can use FilteringTextInputFormatter.
TextField(
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
),

Simply, This could Work:
keyboardType: TextInputType.number,

Related

How to make sure textformfield input is a number(positive or negative or double)

Hi im facing the issue when converting user input to a double example when i input -3 i faced with an FormatException (FormatException: Invalid double -). How can i deal with this? Furthermore how can i prevent user from entering 2 - or . ( 3.3.3 or 3- or --3)as this will also cause error with the double.parse(). Thanks in advance!
TextFormField(
inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[0-9.-]')),],
keyboardType: TextInputType.number,
onChanged:(value) {
setState(() {
fieldPointX = double.parse(value);
});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'X'
),
),
try using this regExp in your TextFormField widget
TextFormField(
keyboardType: const TextInputType.numberWithOptions(
signed: true,
decimal: true,
),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'^-?\d*.?\d*')),
],
),

How to display keyboard with only numbers?

I want to display keyboard with only numbers, not Operators or any other symbols in keyboard.
I tried filter output already using inputFormatters in textfield.
You will need to add inputFormatters & keyboardType properties to enable keyboard with number inputs only to your TextField widget.
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter.digitsOnly,
],
keyboardType: TextInputType.number,
if its in a TextField use this :
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
or you can use this :
keyboardType: TextInputType.phone,
also you can check this package
check this package numeric_keyboard
Use keyboardType property of TextField
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
Here keyboardType is number so it will accept only number,
inputFormatters will format your input what you are typing.
TextField(
keyboardType: TextInputType.number,
inputFormatters:<TextInputFormatter>.
[FilteringTextInputFormatter.digitsOnly]
decoration:InputDecoration(labelText: 'Number')
),
just use this code.
TextFormField(
keyboardType: TextInputType.numberWithOptions(
signed: true, decimal: true),
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
)

How to include plus symbol to a numeric keyboard

I was wondering how to implement in my application a TextFormField with a numeric keyboard that includes the plus("+") with the intention of including of facilitating the user to input international phone numbers.
TextFormField(
controller: phoneNumberController,
keyboardType: TextInputType.number, // Fix to include plus symbol
),
change type TextInputType.number at TextInputType.phone
TextFormField(
controller: phoneNumberController,
keyboardType: TextInputType.phone, // Fix to include plus symbol
),

Input only even numbers in textField Flutter

so, is it way to do TextFormField where users only can insert even numbers? Now they can't type for example 12 and that's the problem.
child: TextFormField(
inputFormatters: [
FilteringTextInputFormatter.deny(RegExp("[1,3,5,7,9]"))
],
keyboardType: TextInputType.number,
),
The even or odd number can be tested by only looking at the last digit, which need to be even or odd too. So the Regex for odd number runs could be:
"$\s*(\d*[13579]\s*,\s*)*\d*[13579]$"
Use regrex expression as below
child: TextFormField(
inputFormatters: [
FilteringTextInputFormatter.deny(RegExp("$\s*(\d*[13579]\s*,\s*)*\d*[13579]$"))
],
keyboardType: TextInputType.number,
),
For to deny even number use below regrex expression
"$\s*(\d*[02468]\s*,\s*)*\d*[02468]$"

Flutter AutofillHints oneTimeCode doesn't work

I want to catch OTP sms code but it doesn't work on IOS.
I need show the code upper of keyboard.
my code block is;
TextField(
controller: controller,
autofillHints: const <String>[AutofillHints.oneTimeCode],
keyboardType: TextInputType.number,
onChanged: onChanged,
),
From Flutter docs at https://master-api.flutter.dev/flutter/material/TextField/autofillHints.html "Some autofill hints only work with specific keyboardTypes". Try changing the keyboardType. For example:
TextField(
controller: controller,
autofillHints: const <String>[AutofillHints.oneTimeCode],
keyboardType: TextInputType.text,
onChanged: onChanged,
),