-
-
Save neliocaa/6889ef1185d570c4186fd5a13f65f965 to your computer and use it in GitHub Desktop.
Javascript Email Mask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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("[email protected]") ); | |
// Output: my.e***l@m*******.c** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment