I am using TypeScript(Javascript)
How can I partially hide email address like this?
jack.dawson@gmail.com
toj*********n@g****.com
rose.bukater@outlook.com
tor**********r@o******.com
I only found an answer using PHP.
Thanks
I am using TypeScript(Javascript)
How can I partially hide email address like this?
jack.dawson@gmail.com
to j*********n@g****.com
rose.bukater@outlook.com
to r**********r@o******.com
I only found an answer using PHP.
Thanks
As stated above, this really isn't a JavaScript job, but here's something short to get you started:
var censorWord = function (str) {
return str[0] + "*".repeat(str.length - 2) + str.slice(-1);
}
var censorEmail = function (email){
var arr = email.split("@");
return censorWord(arr[0]) + "@" + censorWord(arr[1]);
}
console.log(censorEmail("jack.dawson@gmail.com"));
j*********n@g*******m
Like when the lab technician makes kryptonite for Richard Prior in Superman III i've got to say "i'm not sure what you need this for, but here you go..."
Its kinda long-form but that shows the algorithm better.
function obscure_email(email) {
var parts = email.split("@");
var name = parts[0];
var result = name.charAt(0);
for(var i=1; i<name.length; i++) {
result += "*";
}
result += name.charAt(name.length - 1);
result += "@";
var domain = parts[1];
result += domain.charAt(0);
var dot = domain.indexOf(".");
for(var i=1; i<dot; i++) {
result += "*";
}
result += domain.substring(dot);
return result;
}
This is a really, really bad example. It works and should get you the idea, but man is it bad... and long. You could shorten it, but this is only to give you an idea of how it COULD be done.
https://jsfiddle.net/anoffpt9/2/
function changeMail(str) {
var split = str.split("@");
var letter1 = split[0].substring(0, 1);
var letter2 = split[0].substring(split[0].length - 1, split[0].length);
var newFirst = letter1;
for(i = 0; i < split[0].length - 2; i++) {
newFirst += "*";
}
newFirst += letter2;
var letter3 = split[1].substring(0, 1);
var extension = letter3;
for(i = 0; i < split[1].split(".")[0].length - 1; i++) {
extension += "*";
}
extension += "." + split[1].split(".")[1];
var result = newFirst + "@" + extension;
return result;
}
It's very, very bad but you should get the idea.
EDIT: Made it complete. It works well, but it's so damn long and could probably be trimmed, but there are a lot of factors that play in here (like gmail.com -> g****.com) that you need some more than just adding them.
Oh and this only accounts for mails with one extension. co.uk and subdomains will not work.. so yeah. A lot of factors that play in. It's not that easy ;)