How can I show only the final four digits of an input field using Angular Material and AngularJS?

As a newcomer to Angular Material and Angular JS, I am striving to create an md-input field that displays only the last 4 digits in a masked format, such as "********1234". While my experience is currently limited to using md-input password fields where all characters are hidden or replaced with "", I was able to achieve this functionality using JavaScript. However, I am unsure of how to implement this in Angular JS. Here is the code snippet that worked for me:

$('#cardNum').bind('input', function () { $(this).val(($(this).val().replace(/\d(?=\d{4})/g, ''))); });

Answer №1

If you need to mask a password input, try creating a custom filter specifically for that purpose:

appModule.filter('passwordMask', function() {
    return function (input, num) {
        if (isNaN(num) || num < 1) {
            return String(input).replace(/./g, '*');
        }
        var mask = RegExp('(.{1,' + num + '}$)|.', 'g');
        return String(input).replace(mask, function(hide, show) {
            return show || '*';
        });
    };
});

To use the custom filter, simply add it into your code like this:

<div ng-bind="passwordModel | passwordMask:4" class="password-style"></div>

This method should work well for masking passwords as needed.

Answer №2

Hey @Guedes, would it be feasible to implement this code snippet within an input field?

<input  name="password" type="password" ng-bind="user.password | passwordMask:4" />

    myapp.controller('controller', function ($scope) {
      $scope.user = {
        name: '',
        password: '',
      };
    });

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

Stopping React component click event from bubbling up to document

How do I prevent a click event in a React component from bubbling up to the document? There seems to be an issue that needs fixing! Let's see if we can resolve it on our own. I have a box component with a click event listener, and some link compone ...

How to handle unclickable buttons in Selenium testing

from selenium import webdriver from time import sleep from selenium.webdriver.common.by import By chrome_options = webdriver.ChromeOptions() driver = webdriver.Chrome(r'chromedriver.exe', options=chrome_options) url = 'https://rarible.com/c ...

"Conducting API calls in NextJS: Why is Axios/Fetch returning incorrect results when using the post

I'm trying to use a post method on an API, but for some reason when I call the function it posts to the wrong path of the API. Here is the script I am using for the post: //Creating Order const createOrder = async (data) => { try { co ...

Utilize jQuery ajax to pull in data from an external website

I've been doing some research on using jQuery ajax to extract links from an external website, but I'm a bit lost on where to begin. I'm taking on this challenge just to push my skills and see what I can accomplish. While reading about the S ...

What is the proper way to end a WebSocket connection?

Can a websocket connection be terminated by the server without closing the entire server? If so, how can this be done? Note: My back-end is implemented using NodeJS and I am using the 'ws' websocket module. ...

What is the best way to store and recall user selections from a dropdown menu using PHP?

Newbie in the house. I managed to store my user's choice from a select menu, but now it seems like the validation is not working as expected. How can I resolve this issue? This is the snippet of code I'm using for validation: <?php $color ...

Encountering an Uncaught Error in Angular 1.2.9 despite the inclusion of ngRoute module

Even after ensuring the ngRoute file is included and ngRoute is added to dependencyInjection, I am still encountering the following error: Error: [$injector:modulerr] http://errors.angularjs.org/1.2.7/$injector/modulerr?p0=demoModule&p1=Error….org% ...

Understanding the appropriate roles and attributes in HTML for a modal backdrop element in a TypeScript React project

Trying to create a Modal component using React and TypeScript with a feature that allows closing by clicking outside. This is being achieved by adding a backdrop element, a greyed out HTML div with an onClick event calling the onClose method. Encountering ...

The TextArea element is experiencing issues with the Jquery function

In my asp.net web application, I have implemented a JQuery function to restrict user input characters. $(document).ready(function () { $('input').on('input', function () { var c = this.selectionStart, ...

Exploring the dynamic world through HTML5 canvas and animated objects

Today I am exploring HTML 5 canvas and experimenting with moving 3 circles on the canvas. Based on my research, it looks like I need to continuously redraw the circles (perhaps every 60 milliseconds) and clear out the old circle before rendering the new on ...

Uploading a file from a React contact form using Axios may result in S3 generating empty files

I have set up a test contact form that allows users to upload image attachments. The presignedURL AWS Lambda function is working properly After uploading, the image file (blob) appears as an image in the HTML, indicating successful addition Upon posting t ...

My angularjs app hosted on Heroku is experiencing difficulty caching S3 images in the browser, despite setting cache-control headers

I am currently facing an issue with enabling browser caching for S3 images. My AngularJS app is deployed on Heroku and utilizes images stored in an S3 bucket. Despite my efforts, the browser does not cache the images as intended. Instead, it repeatedly req ...

Is XML parsing used by AJAX?

Is an XML parser used by AJAX? If so, where is it used? I believe that the client-side JavaScript engine utilizes the DOM parser to extract necessary information from the XML document received from the server. Could it be possible that AJAX doesn't us ...

prioritizing the loading of the header in an Angular application before proceeding to load the main content

My website has a basic layout shown below: |-------------------| | HEADER | |___________________| |------||-----------| | side || Main | | bar || Content | | || | |------||------------ For routing and states, I am using ...

Is my implementation of Model and Views in backbone.js accurate?

I'm new to backbone.js and I've just created my first page. I'm curious to know if I'm headed in the right direction with my approach (if there even is a "correct" way in software development). Is there a way to automatically bind mode ...

When an HTML input button within a table fails to trigger any jQuery function upon being clicked

I currently have a table set up with multiple rows and columns. <table style="width: 100%;" class='table_result'> <tr><td>Text</td><td>Text</td><td>Text</td><td>Text</td> ...

How come my total isn't zero after I get a 1 on the dice roll?

I've been working on a dice game where if either of the dice rolls a 1, the score is set to 1. However, I'm having trouble getting that part of my code to work properly. I'm confident that everything else is functioning correctly. v ...

Establishing limits for a division using keyboard commands

Alright, I've decided to remove my code from this post and instead provide a link for you to view it all here: (please disregard any SQL errors) If you click on a Grid-Node, the character will move to that position with boundaries enabled. Draggi ...

What is the best way to adjust the fill animation of an inline svg based on the movement of the mouse within the parent div?

Can the fill of an inline SVG path be animated to change as the mouse moves across the page? ...

Tips for converting numbers in JavaScript and troubleshooting issues when trying to filter out non-numeric characters using Tel input commands

After finding this answer on how to convert digits in JavaScript, I attempted to apply the method to convert numbers in a phone number field with tel input type. function toEnglishDigits(str) { const persianNumbers = ["۱", "۲", &quo ...