How can I display only part of an email address using JavaScript?

Let’s keep it real, I’m not good at JavaScript. I really haven’t used it long enough to be good at it. I do get by just fine though and I like to use jQuery as it seems so much easier to me. Perhaps it’s the document. stuff which I find to be odd?

Besides my current job at Rodale I volunteer as the webmaster for Gotham Volleyball in NYC. They have a pretty good system for registration and one that is customized to fit their tryout needs. While their tryout process is how teams are formed and is as unique to them. While their league has expanding their registration system has not.

So here we are in 2013. Gotham Volleyball has had a training program for teams for several years now and keeping track of it all on paper, pre 1999 style. I’m in the midst of programming a registration system for this program and I need a JavaScript function that protects someones privacy by hiding parts of their email address.

I thought this would be easy to find on the Internet, I was WRONG! I scoured around and found a post about how this wouldn’t protect it from spam bots; well that’s not a problem here as it’s behind a login and the email addresses are coming from an ajax service.

Since I’m not the best at JavaScript I assume what I’ve done sucks. Sucks how bad though? I have no idea. It works, gets the job done. Is it inefficient? I’m not sure, maybe that for loop sucks and can be done a faster way?

Here is the code that I came up with, which I call partial email since it displays bits an pieces of an email address.

function partialEmail(email) {
var lastLetter, firstLetter, username;
lastLetter = '';
// get the first part of the email address (to the left of the @)
username = email.substr(0, email.indexOf("@"));
firstLetter = username.substr(0,1);
if (username.length > 1) {
// if the length is over 1 then get the last letter before the @
lastLetter = username.substr(-1);
}
var filler = '';
if (username.length > 2) {
for(var i=2;i<username.length;i++) {
    filler = filler + '*';
}
}
var hiddenUsername = firstLetter + filler + lastLetter;
// get the email domain *note* this returns the @ symbol i.e. @gmail.com
var domain = email.substr(email.indexOf("@"));
// split on the . so we can separate out just the domain
var domainPieces = domain.split('.');
domain = domainPieces[0];
var domainfirstLetter = domain.substr(0,2);
if (domain.length > 1) {
  // if the length is over 1 then get the last letter before the @
  domainlastLetter = domain.substr(-1);
}

filler = '';
if (domain.length > 3) {
for(i=3;i<domain.length;i++) {
filler = filler + '*';
}
}
var topLevel = domainPieces[domainPieces.length - 1];
return (hiddenUsername + domainfirstLetter + filler + domainlastLetter + '.' + topLevel);
}

So there you  – my perhaps clunky JavaScript code to display, or hide, partial pieces of an email address.

Example Input => Output

[email protected] => j*******2@m********r.com

Let the flaming begin – leave comments below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.