flutter-masked-text
flutter-masked-text copied to clipboard
Get text with no mask
It is do not possible to get the text with no mask, example: var controller = new MaskedTextController(text: '', mask: '000-000'); controller.updateText('123456'); print(controller.text); //123-456
but if do you need the value like this ?: print(controller.text); //123456
I have the same problem, my field receives a mask of type '000.000.000-00' but in the database I need to write only '00000000000' as I would do to obtain the unformatted value?
I tried to do it in several ways with the updatemask but it clears the string:
Example:
print (myController.text); print (myController ("edtCpfcnpj"). mask); myController ("edtCpfcnpj"). updateMask ('00000000000'); print (myController.text); print (myController.value);
Result:
I / flutter (22318): 102.710.347-26 I / flutter (22318): 000.000.000-00 I / flutter (22318): I / flutter (22318): TextEditingValue (text: ┤├, selection: TextSelection (baseOffset: 0, extentOffset: 0, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange (start: -1, end: -1 ))
I did a test here but it doesn’t apply to all cases, but I believe it already serves as a base, I’ll check and see if it improves I hope it will serve as a basis for your package:
String get rawNumberValue { String localMask = this.mask; String localText = this.text; for (int i = 0; i < localMask.length; i++) { var aux = localMask.substring(i, i + 1); if ((localMask.substring(i, i + 1) != '0') && (localMask.substring(i, i + 1) != 'A') && (localMask.substring(i, i + 1) != '@') && (localMask.substring(i, i + 1) != '*')) { localText = localText.replaceFirst(aux, '|'); } } return localText.replaceAll('|', ''); }
but if mask has the character '|' going to be a problem
or "Only for numbers"
String get numberValue{ String localMask = this.mask; for (int i = 0; i < localMask.length; i++) { var aux = localMask.substring(i, i + 1); if ((localMask.substring(i, i + 1) != '0') && (localMask.substring(i, i + 1) != 'A') && (localMask.substring(i, i + 1) != '@') && (localMask.substring(i, i + 1) != '*')) { localMask = localMask.replaceFirst(aux, '0'); } } return this._applyMask(localMask, this.text); }
I did a test here but it doesn’t apply to all cases, but I believe it already serves as a base, I’ll check and see if it improves I hope it will serve as a basis for your package:
String get rawNumberValue { String localMask = this.mask; String localText = this.text; for (int i = 0; i < localMask.length; i++) { var aux = localMask.substring(i, i + 1); if ((localMask.substring(i, i + 1) != '0') && (localMask.substring(i, i + 1) != 'A') && (localMask.substring(i, i + 1) != '@') && (localMask.substring(i, i + 1) != '*')) { localText = localText.replaceFirst(aux, '|'); } } return localText.replaceAll('|', ''); }
but if mask has the character '|' going to be a problem
or "Only for numbers"
String get numberValue{ String localMask = this.mask; for (int i = 0; i < localMask.length; i++) { var aux = localMask.substring(i, i + 1); if ((localMask.substring(i, i + 1) != '0') && (localMask.substring(i, i + 1) != 'A') && (localMask.substring(i, i + 1) != '@') && (localMask.substring(i, i + 1) != '*')) { localMask = localMask.replaceFirst(aux, '0'); } } return this._applyMask(localMask, this.text); }
Use regex: RegExp exp = RegExp(r"[^0-9]"); return text.replaceAll(exp, '');
I did a test here but it doesn’t apply to all cases, but I believe it already serves as a base, I’ll check and see if it improves I hope it will serve as a basis for your package: String get rawNumberValue { String localMask = this.mask; String localText = this.text; for (int i = 0; i < localMask.length; i++) { var aux = localMask.substring(i, i + 1); if ((localMask.substring(i, i + 1) != '0') && (localMask.substring(i, i + 1) != 'A') && (localMask.substring(i, i + 1) != '@') && (localMask.substring(i, i + 1) != '')) { localText = localText.replaceFirst(aux, '|'); } } return localText.replaceAll('|', ''); } but if mask has the character '|' going to be a problem or "Only for numbers" String get numberValue{ String localMask = this.mask; for (int i = 0; i < localMask.length; i++) { var aux = localMask.substring(i, i + 1); if ((localMask.substring(i, i + 1) != '0') && (localMask.substring(i, i + 1) != 'A') && (localMask.substring(i, i + 1) != '@') && (localMask.substring(i, i + 1) != '')) { localMask = localMask.replaceFirst(aux, '0'); } } return this._applyMask(localMask, this.text); }
Use regex: RegExp exp = RegExp(r"[^0-9]"); return text.replaceAll(exp, '');
You have to remember that it’s not just a number, the return may be letters and characters ....