Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get text with no mask #55

Open
marcosradix opened this issue Jun 3, 2020 · 4 comments
Open

Get text with no mask #55

marcosradix opened this issue Jun 3, 2020 · 4 comments

Comments

@marcosradix
Copy link

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

@claudneysessa
Copy link

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 ))

@claudneysessa
Copy link

claudneysessa commented Jul 6, 2020

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);
}

@rodrigorafaeldamaceno
Copy link

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, '');

@claudneysessa
Copy link

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 ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants