Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask email in javascript

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

like image 329
Senthil RS Avatar asked Aug 31 '16 11:08

Senthil RS


People also ask

How do I mask an email in JavaScript?

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.

What is masking in JavaScript?

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.

How do you add JavaScript to an email?

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 ...


2 Answers

You can use a regular expression based replacement:

var maskid = myemailId.replace(/^(.)(.*)(.@.*)$/,
     (_, a, b, c) => a + b.replace(/./g, '*') + c
);

Be careful:

  • to do it server side, as any client side replacement could be overturned by the user.
  • that not all email adresses are matching your requirement
  • that when it matches, it could also not be hiding much
like image 173
Denys Séguret Avatar answered Nov 02 '22 13:11

Denys Séguret


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(''))
like image 33
tonmoy122 Avatar answered Nov 02 '22 15:11

tonmoy122