Skip to content

Instantly share code, notes, and snippets.

@gabrielfroes
Created February 21, 2018 14:57
Show Gist options
  • Select an option

  • Save gabrielfroes/282b2f38c7888eaacfc07ca3997e72fa to your computer and use it in GitHub Desktop.

Select an option

Save gabrielfroes/282b2f38c7888eaacfc07ca3997e72fa to your computer and use it in GitHub Desktop.
Javascript Email Mask
/*
Create a Mask in an email address
This function create a mask using a valid email address.
This is usefull when someone need to confirm the email used in a system
Author: Gabriel Froes - https://gist.github.com/gabrielfroes
*/
function emailMask(email) {
var maskedEmail = email.replace(/([^@\.])/g, "*").split('');
var previous = "";
for(i=0;i<maskedEmail.length;i++){
if (i<=1 || previous == "." || previous == "@"){
maskedEmail[i] = email[i];
}
previous = email[i];
}
return maskedEmail.join('');
}
// Usage:
// console.log ( emailMask("my.email@mydomain.com") );
// Output: my.e****@m*******.c**
@yhau1989
Copy link
Copy Markdown

excellent code 👌

@carlosavellar
Copy link
Copy Markdown

Excelwnte

@taitran26
Copy link
Copy Markdown

Perfect code, thanks!!! 💪

@neliocaa
Copy link
Copy Markdown

neliocaa commented Oct 3, 2022

I add some changes to show one char before @

function emailMask(email: string) {
var maskedEmail = email.replace(/([^@\.])/g, '*').split('')
var previous = ''
let j = 0
for (let i = 0; i < maskedEmail.length; i++) {
if (i <= 1 || previous === '.') {
maskedEmail[i] = email[i]
} else if (maskedEmail[i] === '@') {
j = i - 1
maskedEmail[i] = email[j] + email[i] + email[i + 1]
}
previous = email[i]
}
return maskedEmail.join('')
}

@Gaius-Okoase
Copy link
Copy Markdown

I'm trying to do the same thing without regex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment