Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

textbox

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?

like image 457
cheol.lui Avatar asked Oct 05 '22 09:10

cheol.lui


1 Answers

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"    
like image 154
Hjalmar Z Avatar answered Oct 13 '22 12:10

Hjalmar Z