drivingLicenseFormatter static method
drivingLicenseFormatter formats input as a driving license number (e.g., XX-00-00-000000-000000).
Implementation
static List<TextInputFormatter> drivingLicenseFormatter() {
return [
TextInputFormatter.withFunction((oldValue, newValue) {
String newText = newValue.text.replaceAll(RegExp(r'\s+\b|\b\s'), '');
if (newText.length > 15) {
newText = newText.substring(0, 15);
}
final StringBuffer buffer = StringBuffer();
for (int i = 0; i < newText.length; i++) {
buffer.write(newText[i]);
final int index = i + 1;
if ((index == 2 || index == 4 || index == 6 || index == 12) &&
index != newText.length) {
buffer.write('-'); // Add hyphen at specific positions
}
}
final String formattedText = buffer.toString();
return TextEditingValue(
text: formattedText,
selection: TextSelection.collapsed(offset: formattedText.length),
);
}),
];
}