Javascript email mask

I'm new to javascript, I want to hide email id in js

How mynewemail@gmail.com should be disguised as m ******** l@gmail.com . how can i achieve in js. My below code does not work in browser

var maskid = ""; var myemailId = " mynewemail@gmail.com "; 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 this requirement in JS.

thanks

+7
javascript jquery
source share
4 answers

You can use regular expression replacement:

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

Be careful:

  • do this on the server side, since any replacement of the client side can be overturned by the user.
  • that not all email addresses match your requirements.
  • when he matches, he also cannot hide much
+8
source share

Try the following:

 var maskid = ""; var myemailId = " mynewemail@gmail.com "; 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; console.log(maskid) 
+2
source share

Here is another way to do this.

 var maskedid = ""; var myemailId = " mynewemail@gmail.com "; var index = myemailId.lastIndexOf("@"); var prefix = myemailId.substring(0, index); var postfix = myemailId.substring(index); var mask = prefix.split('').map(function(o, i) { if (i == 0 || i == (index - 1)) { return o; } else { return '*'; } }).join(''); maskedid = mask + postfix; console.log(maskedid); 
0
source share

I checked the code in IE 8. The code share is working as expected. Please share the version you checked. Also make sure that the β€œEnable the Allow active content to work in files on My Computer” box is checked.

Steps: 1) In Internet Explorer, select the Tools menu, then Internet Options. 2) In the Internet Options dialog box, select the Preview tab. Scroll to the security settings. Select the Allow running active content in files on My Computer check box.

0
source share

All Articles