September 22, 2012

mask output of the input field

Question by telexper

is there any way to mask the output of the input field once passed to the next page

Enter Card : 123456789011

after the data has been passed onto the next page
when displayed it should look like this

Card info: ********9011

the first 8 digits was converted to asterisk and
the last 4 digits of the card is visible.

Answer by Rhyono

If you’ve already ensured that the card number is a valid length:

card = "123456789011";
output = "********" + card.substring(card.length-4);

is all you’ll need. Output will now be as you wanted, and although Starx’s answer is dynamic: it’s also overkill.

Answer by Starx

Something like this

var card = "123456789011";
var str = "";

//Create the "*" for exactly 4 digits short
for(var i=1; i <= card.length-4; i++) {
   str += "*";
}
//Join the asterisk and last 4 characters
ecard = str + card.substr(card.length-4);

console.log(ecard);

Demo

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!