0

I have only recently started studying Xamarin and I need your help. I saw something what I want on this link How can I create effect hide numbers the credit card except the last 3 numbers

But I do not how I can do it on C#.

enter image description here

Philipp Ape
  • 519
  • 3
  • 13
Maria Kamenskyh
  • 467
  • 1
  • 3
  • 21
  • 1
    If you are in this situation, i don't want to use your app, and its definitely not PCI DSS compliant. Anyway, there is not enough information to answer this question, you need to show code. For instance, what control/xmal are you using here? how are you using it? why not just hardwire the start, and just show the last 4 digits, or is this an image? – TheGeneral Jul 15 '21 at 06:52
  • I'm just learning and want to ask how to make such a mask as in the picture.Do you recommend just display these circles and display the last 4 digits of the card? – Maria Kamenskyh Jul 15 '21 at 06:55
  • `masterCard[0] = "*"`, `masterCard[1]="*"`, ... maybe do it in a loop? – Thomas Weller Jul 15 '21 at 06:56
  • You need to show how you display that now. The solution to change text before displaying will be very different from bimap manipulation. – Sinatr Jul 15 '21 at 07:11

1 Answers1

1

You have to create converter for this, here is in example. Just replace what you want in your logic in this and it will work.

public class CreditCardNumberValueConverter : IValueConverter
{
  public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    var builder = new StringBuilder(Regex.Replace(value.ToString(), @"\D", ""));

    foreach (var i in Enumerable.Range(0, builder.Length / 4).Reverse())
      builder.Insert(4*i + 4, " ");

    return builder.ToString().Trim();
  }

  public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    return Regex.Replace(value.ToString(), @"\D", "");
  }
}

Then use it from xaml like:

Text="{Binding CardNo, Converter={StaticResource CreditCardNumberValueConverter}}"

2 Solution:

string MaskDigits(string input)
{
    //take first 8 characters
    string firstPart = input.Substring(0, 8);

    //take last 4 characters
    int len = input.Length;
    string lastPart = input.Substring(len - 4, 4);
    string maskedDigits = new String('*', 4);

    return firstPart + maskedDigits;
}
Divyesh
  • 2,085
  • 20
  • 35