make TextField accept only numbers and + sign in flutter - flutter

I want the field to allow the entry of numbers and the + sign only but all I could do was make the field allow the entry of numbers only and I couldn't make it accept the + sign
here is my code
TextField(
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(
"[0-9]",
),
),
],
),

TextField(
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(
r'^\+?\d*',
),
),],
)
This will accept an optional + sign at the begining and the rest are numbers. The same as international phone numbers.

TextField(
keyboardType:TextInputType.numberWithOptions(decimal:true,signed:true),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9.,]+')),
],
)
,
just add this.

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*')),
],
),

Dart flutter TextField to accept only digit and decimal

I have TextFormField and i want to allow only number and 1 "." and also only 2 digit after decimal.
I have set below things :
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.allow((RegExp("[0-9.0-9]"))) ],
But this one allows me to enter multiple "." and unlimited after decimal.
I want to allow digits and only 1 "." and only 2 digits after "."
How can i do that?
I got it. It should be
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d*')) ],
You can use the
onChanged : (val) {
// your logic here
}
eg: -
TextFormField(
controller: numberController,
decoration: const InputDecoration(
label: Text(
'Enter Number'
),
),
keyboardType: TextInputType.number,
onChanged: (val){
if(val.indexOf('.') != val.lastIndexOf('.')){
numberController.text = val.substring(0, val.length -1);
showDialog(context: context, builder: (ctx) => AlertDialog(
title: const Text('Please enter valid number'),
actions: [
IconButton(
onPressed: (){
Navigator.of(context).pop();
},
icon: const Icon(Icons.close))
],
));
}
},
),
to check the values dynamically. You can use, control and change your values and fields there.
or you can use the simplest solution from Alex

How to accept only numbers in TextFormFields?

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,

How to prefix two decimal number in textfield

I'm new with flutter and wondering is it possible to prefix the number with decimal in textfield. The prefix number is 0.00 and should not delete it.
<blockquote class="imgur-embed-pub" lang="en" data-id="VnFQQlo">View post on imgur.com</blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
So far i have tried something like limited two decimal entry.
CupertinoFormRow(
child: CupertinoTextFormFieldRow(
controller: controller.price,
onChanged: (value) {
controller.calTotal(controller.qty.text, value);
},
placeholder: "Enter item price",
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'^\d+\.?\d{0,2}')),
],
),
prefix: Text('0.00'),
),
Use currency_text_input_formatter
CupertinoFormRow(
child: CupertinoTextFormFieldRow(
controller: controller.price,
onChanged: (value) {
controller.calTotal(controller.qty.text, value);
},
placeholder: "Enter item price",
inputFormatters: [
CurrencyTextInputFormatter(decimalDigits: 2),
],
),
),

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]$"