1

How to write like a helper method for email address to display with sub mask asterisk.

If user email address is like "john.a@stackoverflow.com", But in view I want to display joh***@***.com

<%= sub_masked_email("john.a@stackoverflow.com") %>
##joh***@***.com
prasannaboga
  • 1,004
  • 1
  • 14
  • 36

1 Answers1

3

So you can write a helper method which can be reused multiple time by calling it on any string. Here is what I came up with below:

helper_method

def sub_masked_email(string)
  string.gsub(/(?<=.{2}).*@.*(?=\S{2})/, '****@****')
end

calling it on any string

sub_masked_email("john.a@stackoverflow.com")

output

"jo*****@****om"

So this method shows the first two letters and shows the last two letters and replaces all other text with ****@****

Just a pointer to what you need and can still be better than this.

Afolabi Olaoluwa
  • 1,898
  • 3
  • 16
  • 37