How to hide e-mail address from an unauthorized user in JavaScript?

JavascriptObject Oriented ProgrammingProgramming

Hiding an e-mail address

The following steps are to be followed to hide our e-mail from unauthorized users. 

  • In every email address '@' symbol is common so try to remove it from the email address using split() method. In the following example after splitting the email(batman@gmail.com) we get the result as batman, gmail.com.
  • Divide the result in to 2 parts(split1 and split2). 
  • Using substring() method remove some of string from split1 and join resulted part with split2 using '...@'. 
  • Return the joined part as the final output. In our example the resulted output is "bat...@gmail.com".

Example

Live Demo

<html>
<body>
<script type="text/javascript">
   newEmail = function (email) {
      var split = email.split("@");
      var split1 = split[0];
      var avg = split1.length / 2;
      split1 = split1.substring(0, (split1.length - avg));
      split2 = split[1];
      return split1 + "...@" + split2;
   };
   document.write(newEmail("batman@gmail.com"));
</script>
</body>
</html>

output

bat...@gmail.com
raja
Published on 21-Jun-2019 19:23:57
Advertisements