const last4Digits = mobileNumber.slice(-2);
const maskedNumber = last4Digits.padStart(mobileNumber.length, "*");
Do you have a question?
yes, I wanna get the mobile number like “9********87” in this format how to do that with JS (note:building this app in react)
what have you tried so far? what results do you have with that?
If you know the length of the string, you could use substrings along with a hardcoded "*******"
string. If the phone number can be of varying length, you can use .repeat()
to create your masking string.
You can’t magically unmask that number. That would defeat the purpose of their privacy protections.
building an app it’s a needed fuctionality there, nothing magic about it
Please use words instead of pictures.
What do you have? (Do you have the full, unmasked number?)
What do you want? (Are you making the masked number?)
What have you tried?
It looks like you already have code to mask the number. You can’t magically recreate the unmasked number from the masked string. That defeats the purpose of masking the number.
Are you trying to reproduce the effect of replacing some numbers with *
or are you trying to figure out what numbers are being replaced by *
in someone else’s application?
If the latter, that logic is happening on the server, not in the client so you would need to do some malicious hacking to gain access to that information (which is hopefully well protected).
i want to un mask the first two numbers of the string
no , I’m creating a mobile login for an app , i want to give them a message that OTP has sent to your number in this format 91*******89
What do you have so far? What part isn’t working? If you are trying to remove part of a number, you’ve already shown one method of doing that. If you can share with us the relevant code and point out what part you need help with, we might be able to help you.
So you have access to the entire number and you just need to replace all but the first two and last two numbers with asterisks? This is a pretty simple regex. Do you think you could give it a try and then if you can’t get it working you can show us what you have tried and we can help. I’ll get you started:
let number = "123456789";
let maskedNumber = number.replace(...);
You fill in the args for the replace
method.
const mobileNumber = useSelector(
(state: ReduxState) => state?.login?.mobileNumber
);
const last4Digits = mobileNumber.slice(-2);
const maskedNumber = last4Digits.padStart(mobileNumber.length, "*");
<p className="otp_para">
Please enter the 6 digit code sent to{" "}
<span className="font-weight-bold">{maskedNumber}</span> to verify
your phone number.
</p>
Your variable name here is contradictory.
So this gets the last two digits. Where are you trying to get the first two digits?
Yes the it gives the output as i have shown in the pic
If you want to replace the middle numbers with *
and leave some at the beginning and end exposed, then you can either use regular expressions (as suggested by @bbsmooth ) or you could construct a new string. If you want to avoid regular expressions I would suggest using .substring()
to get the beginning and end of the number:
Have you tried anything to get the first two digits?
I need to clarify my suggestion here. It is an easy regex if the length of the number is always the same, for example, if the number is always 10 digits, because then you know exactly how many asterisks you will need. If the length of the number is variable then it becomes a little trickier and I needed to solve it with a little more than a simple regex. Not super hard, but not as simple as I made it out to be.