Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing only last 4 digits in a textbox

Tags:

string

c#

I have a textbox in which I am showing credit card or banking info. I want it to be masked ( in the code behind page_load event) so that user can see something like this: xxxxxxxxxxxx-9999.

Eg: string creditcard = 1234567812345678

I want to show like this: xxxxxxxxxxxx5678

Thanks!

like image 617
RG-3 Avatar asked May 27 '11 19:05

RG-3


2 Answers

Something like this might work for variable length text:

// create 4 less x's than the credit card length.
// then append that to the last 4 characters of the credit card
new string('x', creditcard.Length - 4) + creditcard.Substring(creditcard.Length - 4);
like image 100
Greg Avatar answered Dec 21 '22 22:12

Greg


"xxxxxxxxxxxx" + creditcard.Remove(0,12)

As ISO/IEC 7812 credit card numbers have 16 digits.

If the credit card is not ISO/IEC and has another length please use greg's answer. Like AmEx with 15 digits and Diner's with 14. (I wasn't even aware of this as in Europe AmEx is not that common.)

like image 35
Hyperboreus Avatar answered Dec 21 '22 22:12

Hyperboreus