Conceal the message using star symbols

I need help figuring out how to hide a certain type of string input from the user, and then use Angular data binding to display it in another component with part of the data masked with asterisks. I'm not very skilled in JavaScript, so I'm wondering if it's possible to achieve this using CSS instead?

            <p class="subtitle">
                We have sent an email to<br />
                <b>{{ capturedEmail }}</b> with instructions<br />
                on how to recover your password.<br />
                Once you reset your password, you will be able to log in<br />
                to the platform with your credentials.
            </p>

The code snippet above shows where I want to apply this feature to the {{ capturedEmail }} property

For example:

[email protected] -> C***@mail.com

Answer №1

If you're looking to obfuscate email addresses in Angular, you can achieve this using a custom Pipe. For a practical demonstration, here is a Stackblitz Demo showcasing how it's done.

<h1>{{ email | obfuscateEmail }}</h1>
@Pipe({ name: 'obfuscateEmail' })
export class ObfuscateEmailPipe implements PipeTransform {

  transform(value: string): string {
    return value 
      ? value.replace(/\B.+@/g, (c, ) => c.split('').slice(0, -1).map(v => '*').join('') + '@') 
      : value;
  }

}

Result

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e1edf0f0e7edc2efe3ebeeace1edef">[email protected]</a>         >    c*****@mail.com
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cea4a1a6a0e0b9a7ada5fcfc8ea9a3afa7a2e0ada1a3">[email protected]</a>   >    j**********@gmail.com

Answer №2

Essentially, the aim is to conceal the email using asterisks, so by passing any email to the following code snippet, your issue will be resolved.

Give this JavaScript code a try:

const capturedEmail = function (emailAddress, callback) {
    function mask(str) {
        var strLen = str.length;
        if (strLen > 4) {
            return str.substr(0, 1) + str.substr(1, strLen - 1).replace(/\w/g, '*') + str.substr(-1,1);
        } 
        return str.replace(/\w/g, '*');
    }
    return emailAddress.replace(/([\w.]+)@([\w.]+)(\.[\w.]+)/g, function (m, p1, p2, p3) {      
        return callback(null,mask(p1) + '@' + p2 + p3);
    });
     
}
 
capturedEmail('random string <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f1f7e1f6eae5e9e1c4e1fce5e9f4e8e1aae7ebe9">[email protected]</a> test', function(err,res){
console.log(res);
});

Result: u*******<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfdaffdac7ded2cfd3da91dcd0d2">[email protected]</a>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem with Andular's datepicker functionality and local change detection

Currently, we are facing a challenge where local change detection is necessary for our component to improve performance, but it causes issues with the mat datepicker overlay. When navigating with arrow keys within the overlay and changing the month or year ...

Got any ideas for Laravel 8's One to One messaging feature?

I am looking to create a real-time one-to-one messaging system for the users of my application. I anticipate around 10,000 users. Instead of utilizing web sockets or similar solutions, I am currently using Livewire for other features. My initial thought wa ...

What can be done to ensure that the a href tag is functioning as clickable?

Having an issue with my HTML and CSS code for a notification dropdown box. I am unable to click the tag, even after attempting to use JavaScript. Can't seem to figure out what's causing this problem. Any advice on how to make the tag clickable? ...

What is the best way to pass a mask without using a plug-in when dealing with an input value in Vue.js?

As someone who has previously implemented the function using pure JavaScript, I am now faced with the challenge of incorporating it into vue.js and determining which lifecycle hooks are best suited for this task. This issue arises as I am a beginner prepar ...

Is there a way for me to gain entry to this array in vuejs?

Can anyone help me with accessing the objects in this array? I am using laravel, inertiajs, and vuejs. I am passing a variable from a laravel controller to a vuejs component with inertia.js. https://i.stack.imgur.com/p7yjL.png https://i.stack.imgur.com/y ...

I am encountering an issue with my function where I aim to prevent the creation of a node using duplicate coordinates

Trying to avoid creating a node with existing coordinates, I implemented a check in my code. The check is supposed to determine if there are any nodes with the same coordinates already present. However, it seems that the check is not working as expected an ...

Ensuring a response is sent back only after a loop has completed in NodeJs

Struggling to properly format the logic in order to send back a fully populated array of objects after a GET request to a specific URL. The goal is to take the "connections" array from the user making the request, extract the connection's name and pic ...

Issue with Bootstrap 3 dropdown not receiving updates

My goal is to dynamically populate a bootstrap 3 dropdown menu from a MySQL database. Everything works perfectly the first time I load the data. However, when I add more rows to the database, they do not automatically show up in the dropdown. Only after ...

What is the process for converting an array of objects into an array of form groups?

I am looking to utilize the array function of the formbuilder to handle formcontrol arrays. I have a custom object array that I need to integrate into a FormArray, but it seems to only accept FormGroup as arguments. How can I convert my array of custom obj ...

React Router is not compatible with ReactJS app version 18

After using the command npx create-react-app, I've just set up a new ReactJS app which I think is running on React version 18 (feel free to correct me if I'm mistaken). Now, as I attempt to implement a router for this app, I find myself hesitati ...

The Select2 widget passes parameter term spaces to the ajax request as a plus sign "+" instead of %20

I have been working on an application that connects to SAP's service layer and retrieves data using REST APIs. I am using the popular widget select2, but I have encountered a problem. The API query that needs to be made contains a space character arou ...

Having trouble centering my menu bar on CSS, can anyone help?

I'm having trouble getting the buttons inside the menu bar to align properly. Does anyone have any suggestions on how to fix this? I'm not very experienced with coding, so any assistance would be greatly appreciated! .main-navigation { clear ...

I'm currently troubleshooting the code for the Gallery project. The gallery is supposed to have 4x4 grids, but for some reason, the grids are

It seems like I am struggling to identify the exact issue. The display on mobile looks fine but not on desktop. I attempted to tweak the isotope configuration without success. Even manipulating the server-side code didn't reveal any obvious problems. ...

Combine a string and integer in JavaScript without using quotation marks between them

Is there a way to concatenate a string and an integer in JavaScript without getting the ": Here is the code snippet: "<agm-map latitude=" + response.latitude + " longitude=" + response.longitude + "></agm-map>"; What it currently results in: ...

What is the best way to extract information from an API using javascript?

I've been working on a weather app project, and recently made changes to the code so that the longitude and latitude values are based on the user's geolocation. However, I've encountered an issue where the button is no longer functioning. Up ...

Arrange text in a line below neatly in a list

My goal is to line up items on the same row, as shown in the screenshot. I need the opening hours to align consistently in the list. The data is retrieved from a database and displayed using Vue. This is how I currently display the opening hours and days. ...

What is the method to insert a new <input> element after the last input field has been filled in

I recently started working on a form using StackBlitz, but I've hit a roadblock and need some guidance on how to proceed. My goal is to achieve a similar effect like the one shown in this gif: https://i.stack.imgur.com/76nsY.gif and I'd like to ...

Encountering errors in Visual Studio when trying to work with node_modules directories that have a tsconfig

In my current project, there is a tsconfig.json file located in the root directory. Strangely, Visual Studio keeps throwing errors related to other instances of tsconfig.json found in different packages, as shown below: https://i.sstatic.net/T7Co2.png Ev ...

What is the best way to arrange a list of dates without taking into account the year associated with each date?

Currently in my Angular 4 project, I have a list of upcoming birthdays displayed in ascending order, but it is also taking the year into account. What I really need is to sort this list only by the date and month, excluding the year. Could someone please ...

Issue: Unable to access GET request with Express and handlebars

Hello everyone, I'm just getting started with JS/Handlebars and I'm facing an issue trying to display an image from my home.hbs file in VS Code. When I start the server, this is the message I receive: https://i.sstatic.net/wUxB7.jpg Below is th ...