Reputation: 1270
I want to develop a custom user control in WPF which has some sort of mask. this functionality is very like the one provided in many online applications where u enter your credit card number and next time whenever u sign in to your account u see only last for digits of the card number (something like ***4587) but while I display data in this way I want to keep real value unchanged so inside binding I will access full data.
Upvotes: 2
Views: 1151
Reputation: 6961
You could use Regex.Replace with \d to indicate a digit
i.e.
var digits = new Regex(@"\d");
modifiedNumber = digits.Replace(originalNumber, "*");
Or if you want to update the whole set of numbers except for the last group
@"\d{4}-"
Upvotes: 1
Reputation: 1270
OK here is the way I resolved that issue. after working with Card numbers I wanted to work with ID and SN numbers as well so what I did was just wrote little method which takes in string and returns masked value here it is in case someone needs this in feature.
public static string GetMaskedNumber(string unsecuredNumber, char maskChar)
{
return unsecuredNumber.Substring(unsecuredNumber.Length - 4)
.PadLeft(unsecuredNumber.Length - 6, ' ')
.PadLeft(unsecuredNumber.Length, maskChar);
}
Upvotes: 1
Reputation: 1175
You could try something like this:
string originalNumber = textBoxOriginalNumber.Text;
int numberOfDigits = textBoxOriginalNumber.Text.Length;
string hidden = new String('*', numberOfDigits-4);
textBoxModifiedNumber.Text = hidden + originalNumber.Remove(0, numberOfDigits-4);
It's is not an elegant solution but will help you if anybody else give you a better solution. Basically, it takes the original credit card number, counts how many digits it has, removes the "n-4" first digits, then show the * symbol "n-4" times plus the four last digits. This will work no matter how many digits the original number has.
Additionally, I'm not sure if a mask (or the Regex suggested by the other user bellow) would work because (if I understood it well) it would replace the whole number, instead of show the last 4 digits.
Upvotes: 2