Created
October 18, 2017 16:47
-
-
Save brunoportess/70ef2bb53a77d9ffff3a5474effcbbcc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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