I am new for javascript, i want to mask email id in js
Like [email protected] should mask as m********[email protected]. how do i achieve in js. My below code is not working in ie browser
var maskid = "";
var myemailId = "[email protected]";
var prefix= myemailId .substring(0, myemailId .lastIndexOf("@"));
var postfix= myemailId .substring(myemailId .lastIndexOf("@"));
for(var i=0; i<prefix.length; i++){
if(i == 0 || i == prefix.length-1 ) {
maskid = maskid + prefix[i].toString();
}
else {
maskid = maskid + "*";
}
}
maskid =maskid +postfix;
I want to handle in JS is the requirement.
Thanks
const masked = 'r... [email protected]'; We are required to write a JavaScript function that takes in an email string and returns the masked email for that string.
The JavaScript Input Mask or masked textbox is a control that provides an easy and reliable way to collect user input based on a standard mask. It allows you to capture phone numbers, date values, credit card numbers, and other standard format values.
To enable the script, you have to first fill in your email address in the script's eml variable, then include the script in your document, add onload="putEmail(); to the document's <body> tag, and finally put <span class="myemail">(need Javascript for email link)</span> (or something similar) everywhere you want to ...
You can use a regular expression based replacement:
var maskid = myemailId.replace(/^(.)(.*)(.@.*)$/,
(_, a, b, c) => a + b.replace(/./g, '*') + c
);
Be careful:
let str = "[email protected]"
str = str.split('');
let finalArr=[];
let len = str.indexOf('@');
str.forEach((item,pos)=>{
(pos>=1 && pos<=len-2) ? finalArr.push('*') : finalArr.push(str[pos]);
})
console.log(finalArr.join(''))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With