Skip to content

Instantly share code, notes, and snippets.

@neliocaa
Forked from gabrielfroes/emailmask.js
Last active October 3, 2022 22:01
Show Gist options
  • Save neliocaa/6889ef1185d570c4186fd5a13f65f965 to your computer and use it in GitHub Desktop.
Save neliocaa/6889ef1185d570c4186fd5a13f65f965 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
===================================================================
Update 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('')
}
// Usage:
// console.log ( emailMask("my.email@mydomain.com") );
// Output: my.e***l@m*******.c**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment