Computer >> Computer tutorials >  >> Programming >> Javascript

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


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([email protected]) 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 "[email protected]".

Example

<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("[email protected]"));
</script>
</body>
</html>

output

[email protected]