I want have a TextFormField
that formats the input depending on a given separator
and interval
.
E.g. separator = '-'
and interval = 4
the output should be the following:
Input: 1234 Output: 1234
Input 12345678 Output: 1234-5678
Input: 12345678910 Output: 1234-5678-910
The separator should be inserted right when the user types in the 5th character in this example.
I tried multiple things now, but can not get a general working solution. That was my last try:
void _addSeperator(String value) {
final separator = widget.separator!;
final separatorInterval = widget.separatorInterval!;
value = value.replaceAll(separator, ''); // Remove existing separators
int length = value.length;
int separatorCount = (length / separatorInterval).floor();
for (int i = 1; i <= separatorCount; i++) {
int index = i * separatorInterval +
(i - 1); // Calculate the index to insert separator
if (index < length) {
value = value.substring(0, index) +
separator +
value.substring(index, value.length);
}
}
_controller.text = value;
}
I thought this shouldnt be that hard, but I can not make it work. Also didn’t find anything on this. Let me know if you need more info.
2
Answers
It looks like you are trying to format credit card numbers. Flutter already comes with 2 handful formatter that you could use,
FilteringTextInputFormatter
andLengthLimitingTextInputFormatter
. You could also extendsTextInputFormatter
and implement your custom formatting logic.Below is a simple example.
FilteringTextInputFormatter.allow(RegExp(r'd'))
only allows digits.LengthLimitingTextInputFormatter(16)
set the max input length to 16.CreditCardNumberFormatter
is a custom formatter that adds a separator between every 4 digits.You can use the very popular library mask_text_input_formatter to format the text within a specific format. See its documentation on how to use it.