bhrott / flutter-masked-text

A masked text for Flutter.

Home Page:https://pub.dartlang.org/packages/flutter_masked_text

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to get an unmasked value

codegrue opened this issue · comments

For example -

mask: (000) 000-0000
value: (123) 456-7890

unmaskedValue: 123456789

??

Not sure if the best way but this code will strip off the mask:

import 'dart:math';

void main() {
  
  String mask = 'AA (000) @@@-@@@@ **';
  String maskedValue = 'US (555) 123-CALL #';

  print(unmaskValue(mask, maskedValue));  
  // prints US555123CALL#
}

String unmaskValue(String mask, String maskedValue) {
  
  String specialCharacters = '0A@*';
  String unmaskedValue = '';
  
  for (int i=0; i < min(mask.length, maskedValue.length); i++) {
    if (specialCharacters.contains(mask[i]))
      unmaskedValue += maskedValue[i];
  }
  
  return unmaskedValue;
}

"one liner" solution:

var unmaskedPhone = _phone.replaceAll(new RegExp('[^0-9]'), '');

warning: this replaceAll approach may fail in complex cases.

Will be very good if there's a rawText method in the future.

How Get unmasked text?

Will we do it sir :D?