Include Char password in text field except last N Char acter / Limit Masked Char

How to enable Char password in text box except last character N?

I already tried this method

cardnumber.Select((c, i) => i < cardnumber.Length - 4 ? 'X' : c).ToArray()

But it’s so difficult to manipulate, I will transmit the original value of the card in each case, for example Keypress, TextChangeetc.

Is there a way that is easier and more convenient to manage?

+5
source share
1 answer

That should do the trick,

string pw = "password1234";
char[] splitpw;
string cenpw;
int NtoShow;

splitpw = new char[pw.Length];
splitpw = pw.ToCharArray();
NtoShow = 4;
for (int i = 0; i < pw.Length; i++)
{
    if (i < pw.Length - NtoShow)
        cenpw += "*";
    else
        cenpw += splitpw[i];
}

//cenpw: "********1234"    
+4
source

All Articles