Skip to content

Instantly share code, notes, and snippets.

@brunoportess
Created October 18, 2017 16:47
Show Gist options
  • Save brunoportess/70ef2bb53a77d9ffff3a5474effcbbcc to your computer and use it in GitHub Desktop.
Save brunoportess/70ef2bb53a77d9ffff3a5474effcbbcc to your computer and use it in GitHub Desktop.
public class MaskMoney : Behavior<Entry>
{
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += onTextChanged;
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= onTextChanged;
base.OnDetachingFrom(bindable);
}
void onTextChanged(object sender, TextChangedEventArgs args)
{
var entry = (Entry)sender;
entry.Text = formatPhoneNumber(entry.Text);
}
private string formatPhoneNumber(string input)
{
//var digitsRegex = new Regex(@"[^\d]");
var digits = input.Replace(input, "");
if (digits.Length < 3)
return "0," + input;
//return $"0,{input}";
if (digits.Length >= 3 && input.Length <= 5)
return digits.Substring(2, 3) + "," + digits.Substring(0, 2);
//return $"{digits.Substring(2, 3)},{digits.Substring(0,2)}";
//return $"{digits.Substring(4)}.{digits.Substring(2, 3)},{digits.Substring(0, 2)}";
return digits.Substring(4)+"."+ digits.Substring(2, 3) + ","+digits.Substring(0, 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment