I have a input field with a maximum length of 15 character. i want to do something like this
original number :784198012345671 should be: 784********5671
I have a input field with a maximum length of 15 character. i want to do something like this
original number :784198012345671 should be: 784********5671
const original = "784198012345671",
obfuscated = original.substr(0, 3) + "*".repeat(original.length - 7) + original.substr(-4);
You may use substr to get the number groups at the beginning and the end out of the number and then use repeat to fill the asterisks inbetween.
The process to achieving what you want varies depending on whether:
If you want to do this, after the value has been entered the following should do the trick.
Snippet:
var
/* The original value. */
n = "784198012345671",
/* The masked value. */
masked = n.slice(0, 3) + Array(8).join("*") + n.slice(-4);
/* Log the result. */
console.log(masked);
If, instead, you want to mask the input during typing, things get more complicated.
Snippet:
$(".newsocial").on("keyup", function(e) {
/* Turn the value into an array of characters. */
var value = this.value.split("");
/* Iterate over every character. */
value.forEach(function (char, index) {
/* Replace the character with a placeholder when appropriate. */
if (index >= 3 && index <= 10) value[index] = "*";
});
/* Turn the array of chars into a string & assign it to the value of the input. */
this.value = value.join("");
})
<!--- HTML --->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" maxlength="15" id="input-card" class="newsocial">
Replace Integer to String
Iterate the string and replace the position with *:
let number = 123455666777888;
let stringedNum = number.toString();
for(i=0;i<stringedNum.length;i++) {
if(i>5 && i<10) { // change this line as per your need
console.log(i)
stringedNum = stringedNum.replace(stringedNum[i],'*');
}
}
console.log(stringedNum)
Convert your number to string and use string.prototype.substring and string.prototype.repeate to build parts:
var number = 784198012345671;
number = number.toString();
var res = number.substring(0, 3) + '*'.repeat(8) + number.substring(11, 15);
console.log(res);