Add adhesive dashes to the text box

I am looking to add sticky hyphens in a text input field. I have included an image illustrating my requirement. The areas that need fixing are highlighted in red. The first hyphen should appear after every three number inputs, followed by the next one after two inputs, and so on.

Currently, the numbers show up as I click on them, but I want the dashes to be displayed by default.

I attempted using a span element to achieve this, but unfortunately, I was unsuccessful.

<div class="row">
        <p id="p" style="font-size: 30px;">Enter location:<input style="margin-left: 10px; font-size: 30px;"
            type="text" [value]="codeNum" id="code" placeholder="___-__-__-__-__">
          <button type="button" style="margin-left: 10px; font-size: 30px; border: white 3px solid;"
            class="btn btn-dark btn-lg" (click)="clear()">Clear</button>
          <button type="button" style="margin-left: 10px; font-size: 30px; border: white 3px solid;"
            class="btn btn-dark btn-lg" (click)="allClear()">Clear All</button>
        </p>
      </div>
    </div>

    <div class="calculator-keys">
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('0')" value="0">0</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('1')" value="1">1</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('2')" value="2">2</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('3')" value="3">3</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('4')" value="4">4</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('5')" value="5">5</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('6')" value="6">6</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('7')" value="7">7</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('8')" value="8">8</button>
      <button type="button" class="btn btn-light waves-effect" (click)="pressNum('9')" value="9">9</button>
    </div>

https://i.sstatic.net/rRwlX.jpg

Appreciate your help!

Answer №1

If you're looking for a solution that fits your requirements, consider exploring various packages available. For instance, you can check out ngx-mask

npm i --save ngx-mask

After installing the package, you can use it as follows:

<input type="text" mask="000-00-00-00-00" [(ngModel)]="codeNum">

Alternatively, you can display the masked value like this:

<p>{{ codeNum | mask: '000-00-00-00-00' }}</p>

Check out this live example for reference: https://stackblitz.com/edit/angular-ngx-mask-example-zd89hh?file=src%2Fapp%2Fapp.component.html

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

The CSS class is not functioning properly while the ID is working perfectly

A question I have pertains to the navigation menu on my website and its default styling... <nav> <ul id="MenuBar1" class="MenuBarHorizontal"> <li> <a href="#">Home</a> </li> <li> <a class="Menu ...

``It has come to our attention that ALT texts are not displaying properly on popular email clients such as Outlook,

Despite following all the email standards and attempting to style the ALT tag, most major email clients do not display the ALT text when images fail to load. Is there a workaround for this issue or does it primarily depend on the specific email engine bein ...

Map through the index and render the component while closing the tag

I have a piece of code that I'm working on. How can I dynamically add a tag based on the index? My goal is to create a grid with 3 columns. <div> {items.map((row, i) => ( {i % 3 === 0 && <div className={'grid-cont ...

Ensure that the height of the flex body is set to 100% within the ReactNative View

Within my React Native app, I have constructed the following view: <ScrollView style={{ width: 250, height: '100%', backgroundColor: '#000000' }}> <SafeAreaView style={{ flex: 1, flexW ...

Guide to showcasing an image obtained from a Web API in Angular

I have a unique challenge with displaying images on the user interface. The image data is retrieved from a database through a web API, and the image path is stored in the DB's imagePath column. However, when fetching the data via the web API, only the ...

Using PHP, show a specific table row when clicked by matching the ID

I am currently developing an application for a school project that manages tests. This app allows employees to log in, select a client, register clients, and conduct tests with them, all while storing the data in a database. I have successfully implemente ...

Incorporate a fresh attribute to the JSON data in an Angular API response

I'm currently working on updating my JSON response by adding a new object property. Below is an example of my initial JSON response: { "products": [{ "id": 1, "name": "xyz" }] } My goal is to include a new object property ca ...

The issue of incomplete post content display on WordPress pages persists despite making modifications to the style.css file

click here for image descriptionI attempted to make some adjustments in the style.css file but encountered a problem. Now, all the posts on the site are displaying "[...]" after a brief and unformatted snippet of content. Any assistance would be greatly ...

What is the process for updating npm and node version on Windows operating system?

Currently immersed in a finance project, but embarking on a new one where we are facing a node version issue. Any tips on how to avoid this problem? ...

Angular project service file experiencing issues with TypeScript string interpolation functionality

Here is the code snippet for a service in an Angular project: @Injectable() export class FetchDataService { fetch(link){ console.log('This is a ${link}'); } } In my component, I am invoking this method with a string parameter. Upon che ...

Alignment of Submenus in a CSS Vertical Menu

I'm currently facing an issue with my vertical menu and submenu alignment. Despite everything working correctly, the submenu does not align horizontally as expected, instead shifting up against the header div above it. Any assistance to resolve this w ...

Edit: "Submit a binary file with just JavaScript"

I am currently developing a Chrome application that utilizes the HTML5 Filesystem API to enable users to import and synchronize files. A challenge I'm facing is when attempting to sync image files, as they appear to become corrupted during the upload ...

AngularJS - Custom directive to extract a property value from an object

Currently, I am using the following for loop to retrieve the parent category: angular.forEach(queryTicketCategories, function(category) { if(category.id === $scope.ticketCategory.parentId) { $scope.parent = category; } }); I am looking fo ...

The outcome of the .then function is not defined following another .then function

I am struggling with handling async requests in Angular within Ionic4. I have successfully loaded a .json file and saved it into an array, but when trying to edit the array within a loop, my promise resolves before the loop finishes. As a newcomer to Angul ...

Switch between active tabs (Typescript)

I am working with an array of tabs and here is the code snippet: const navTabs: ITab[] = [ { Name: allTab, Icon: 'gs-all', Selected: true }, { Name: sources.corporateResources, Icon: 'gs-resources', Selected: false }, { Name ...

In response to resolving an HTTP header issue with a status of 200 ok during API testing with Postman, what steps can be taken

Hello everyone, I am new to the world of Angular and facing some issues while learning. Following a tutorial on YouTube, I tried to replicate the process with a few modifications. Initially, my get API worked fine when tested with Postman, and the post API ...

The data-src tags are functioning properly in the index.html file, but they are not working correctly in angular

I'm still learning about Angular and JavaScript, so please bear with me if my questions seem silly. I've been trying to add a theme to my Angular project. When I include the entire code in index.html, everything works fine. However, when I move ...

What sets apart Firefox and Chrome when it comes to interpreting the widths of flex items?

Looking to create a column layout using flexbox with three divs that span the full width of their parent container while maintaining a single line height. If the content in the center div overflows, I want it to display an ellipsis at the end. Check out t ...

Angular 5 - Issue with Conditional Validator Functionality Being Ineffective

If the email field is not left empty, then the re-email field must be marked as 'required'. In order to achieve this functionality, I have implemented conditional validators for the re-email field using the code snippet below: HTML <div cla ...

"Using Rails remote link to create a standalone 'button' element, not embedded in a line of text or a

In my current project, I am attempting to create an ajax "button" that mimics the functionality of a typical link_to "content", some_path, remote:true. The goal is to have this button swap out images to indicate different statuses when clicked (e.g. on/off ...