Allow only specific input in TextFormField without validation in Flutter
Solution 1
May try the code below, for your reference https://stackoverflow.com/a/68072967/7972633
Updated version 1
class NumericalRangeFormatter extends TextInputFormatter {
final double min;
final double max;
NumericalRangeFormatter({required this.min, required this.max});
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue,
) {
if (newValue.text == '') {
return newValue;
} else if (int.parse(newValue.text) < min) {
return TextEditingValue().copyWith(text: min.toStringAsFixed(5));
} else {
return int.parse(newValue.text) > max ? oldValue : newValue;
}
}
}
keyboardType: TextInputType.numberWithOptions(),
inputFormatters: [
LengthLimitingTextInputFormatter(5) // only allow 5 digit number
],
Solution 2
You can use validator for a field that is not mandatory in this way :
validator: (value) {
if (value == null || value.isEmpty) {
return null;
}else {
double? num = double.tryParse(value);
if(num == null)
return 'Invalid value';
else if(num < 1 || num > 10)
return 'Please enter value between 1 and 10.000';
}
return null;
},
So, in this way if value is null or empty then we can skip check otherwise perform required check.
GrandMagus
Updated on November 25, 2022Comments
-
GrandMagus over 1 year
I want to allow the user to only put maximum of 5 numbers between 1 and 10.000, but this
TextFormField
is not required and should not be submitted throughForm
validation, but I want to let the user know if he is adding this field, that he can not exceed 10.000 and he must put only numbers from 1 to 10.000. The code for theTextFormField
:TextFormField( keyboardType: TextInputType.number, controller: _number, inputFormatters: <TextInputFormatter>[ FilteringTextInputFormatter.digitsOnly //I have set this so the input is only numbers/digits ], decoration: kTextFieldDecoration.copyWith( hintText: 'Enter number between 1 and 10.000', labelText: 'Number from 1 to 10.000', ), ),
I'm not sure how to achieve this, I used regex validation for the rest of the fields, but since this field is not required, I can't validate it through
Form
validation. Any form of help is appreciated. Thanks in advance!-
Jahidul Islam over 2 yearsyou just want to put num from 1 to 10 with three decimal places and look here stackoverflow.com/questions/70059605/…
-
-
GrandMagus over 2 yearsYes, but I still want to tell the user that he can not exceed the number 10.000, I can still put in let's say 55.000, the number limit is five but the limited number is way off.
-
HeIsDying over 2 years@GrandMagusi have updated my answer, have a try
-
HeIsDying over 2 years
-
GrandMagus over 2 yearsI tested it out, it works actually, you can enter only a 5 digit number that is 10k, but any number from 1-9999 is good. Thanks for the detailed research and information provided.
-
GrandMagus over 2 yearsLike I said in the question above, I need this to limit the user without using validations because this field is not required in the
Form
populated. -
GrandMagus over 2 yearsThe only one more thing I need, if there is a chance to notify the user what is the limit of the input.
-
Shatanik Mahanty over 2 yearsYes that's why I added a return null in case user has not filled the input for this field
-
GrandMagus over 2 yearsAnd also, please reference where to call the
NumericalRangeFormatter
class for people that are having the same issue (inside the inputFormatters[]). Thanks again! -
GrandMagus over 2 yearsOk, this answer is also correct, I just tested it out in multiple options and it works great, but I think I will go with @HeIsDying answer since it limits the user not to exceed those digits at start. I wish I could accept both answers to be honest! Thanks a lot mate once again!
-
Shatanik Mahanty over 2 yearsI also feel @HelsDying answer is much more compact and you should totally go with it. Thanks for your kind works and good luck with the work.