cheol.lui
cheol.lui

Reputation: 379

Enable Password Char in TextBox except last N character / Limit Masked Char

How to enable Password Char in TextBox except last N character?

I already tried this method

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

But it is so hard to manipulate, I will pass original card value in every event like Keypress, TextChange and etc..

Is there I way that is more simple and easy to managed?

Upvotes: 5

Views: 434

Answers (1)

Hjalmar Z
Hjalmar Z

Reputation: 1601

This 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"    

Upvotes: 4

Related Questions