Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to hide e-mail address from an unauthorized user in JavaScript?
Hiding email addresses from unauthorized users helps protect privacy and reduce spam. JavaScript provides several methods to obfuscate email addresses while keeping them readable for legitimate users.
Method 1: Partial Masking with Asterisks
This approach replaces part of the username with asterisks, keeping the domain visible:
<html>
<body>
<script type="text/javascript">
function hideEmail(email) {
var parts = email.split("@");
var username = parts[0];
var domain = parts[1];
if (username.length <= 2) {
return username[0] + "***@" + domain;
}
var visibleChars = Math.ceil(username.length / 3);
var hiddenPart = "*".repeat(username.length - visibleChars);
return username.substring(0, visibleChars) + hiddenPart + "@" + domain;
}
document.write(hideEmail("batman@gmail.com") + "<br>");
document.write(hideEmail("john.doe@company.org") + "<br>");
document.write(hideEmail("a@test.com"));
</script>
</body>
</html>
ba****@gmail.com joh*****@company.org a***@test.com
Method 2: Dots Masking (Original Approach)
The following steps hide the email by removing part of the username and replacing it with dots:
- Split the email address at the '@' symbol to separate username and domain
- Calculate half the length of the username
- Keep only the first half of the username using
substring() - Join the partial username with "...@" and the domain
<html>
<body>
<script type="text/javascript">
function newEmail(email) {
var split = email.split("@");
var split1 = split[0];
var avg = split1.length / 2;
split1 = split1.substring(0, (split1.length - avg));
var split2 = split[1];
return split1 + "...@" + split2;
}
document.write(newEmail("batman@gmail.com") + "<br>");
document.write(newEmail("administrator@company.com"));
</script>
</body>
</html>
bat...@gmail.com admini...@company.com
Method 3: Advanced Obfuscation
For better security, this method hides both username and domain parts:
<html>
<body>
<script type="text/javascript">
function advancedHideEmail(email) {
var parts = email.split("@");
var username = parts[0];
var domain = parts[1];
// Hide username (show first 2 chars)
var hiddenUsername = username.length > 2 ?
username.substring(0, 2) + "***" :
username[0] + "**";
// Hide domain (show first char and extension)
var domainParts = domain.split(".");
var hiddenDomain = domainParts[0][0] + "***." + domainParts[1];
return hiddenUsername + "@" + hiddenDomain;
}
document.write(advancedHideEmail("batman@gmail.com") + "<br>");
document.write(advancedHideEmail("user@yahoo.org"));
</script>
</body>
</html>
ba***@g***.com us***@y***.org
Comparison
| Method | Security Level | Readability | Use Case |
|---|---|---|---|
| Asterisk Masking | Medium | High | User profiles, contact lists |
| Dots Masking | Low | High | Simple obfuscation |
| Advanced Obfuscation | High | Medium | Public displays, forums |
Conclusion
Email obfuscation in JavaScript helps protect user privacy. Choose the method based on your security needs?asterisk masking for better readability, or advanced obfuscation for maximum protection from automated scrapers.
Advertisements
