question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ability to replace/overwrite character in the middle of input instead of inserting

See original GitHub issue

Example project - https://stackblitz.com/edit/angular-5lfprb When I position cursor like that 123-4|56-789 and press any digit, input state becomes 123-40|5-678 - 0 inserted between 4 and 5, 9 is truncated. It would be good to have the ability to replace characters, so input state would be 123-40|6-789.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
mrmokwacommented, Apr 2, 2019

Yeah, got a similar issue with my telephone mask. In Brazil we can have (11) 1 1111-1111 and (22) 2222-2222 for telephone numbers. If a have a mask like (00) 9 0000-0000, when applying the mask, I’ll get (22) 22222-222 instead of (22) 2222-2222.

1reaction
thaylongscommented, May 31, 2019

Yeah, got a similar issue with my telephone mask. In Brazil we can have (11) 1 1111-1111 and (22) 2222-2222 for telephone numbers. If a have a mask like (00) 9 0000-0000, when applying the mask, I’ll get (22) 22222-222 instead of (22) 2222-2222.

I have the same problem

Hi @arthurfrancioni , I solve this with following code:

import {ChangeDetectorRef, Directive, Host, OnInit, Optional, SimpleChange} from '@angular/core';
import {MaskDirective} from "ngx-mask";

@Directive({
  selector: '[telefoneMask]'
})
export class TelefoneMaskDirective implements OnInit {

  mask11 = "(00) 0 0000-0000";
  mask10 = "(00) 0000-00009";
  currentMask = this.mask10;
  currentValue = null;

  constructor(private cdr: ChangeDetectorRef, @Host() @Optional() private mask: MaskDirective) {
  }

  ngOnInit(): void {
    this.mask.maskExpression = this.currentMask;
    this.mask.ngOnChanges({"maskExpression": new SimpleChange("", this.currentMask, true)});
    this.mask.registerOnChange((telefone) => {
      if (telefone != null && this.currentValue !== telefone) {
        this.currentValue = telefone;
        this.processInputChange(telefone);
      }
    });
    this.cdr.detectChanges();
  }

  private processInputChange(telefone: string) {
    if (telefone.length <= 10) {
      if (this.currentMask != this.mask10) {
        this.currentMask = this.mask10;
        setTimeout(() => {
          this.mask.ngOnChanges({"maskExpression": new SimpleChange(null, this.mask10, false)});
        }, 50);
      }
    } else {
      this.currentMask = this.mask11;
      setTimeout(() => {
        this.mask.ngOnChanges({"maskExpression": new SimpleChange(null, this.mask11, false)});
      }, 50);
    }
  }
}

To use:

<input class="form-control" [(ngModel)]="telefone" mask telefoneMask>

Is this code still valid? Did a quick test here and did not work.

Hi, try this version (with: this.control.control.setValue(this.currentValue)😉:

import {ChangeDetectorRef, Directive, Host, OnInit, Optional, SimpleChange} from '@angular/core';
import {MaskDirective} from "ngx-mask";
import {NgControl} from "@angular/forms";

@Directive({
  selector: '[telefoneMask]'
})
export class TelefoneMaskDirective implements OnInit {

  mask11 = "(00) 0 0000-0000";
  mask10 = "(00) 0000-00009";
  currentMask = this.mask10;
  currentValue = null;

  constructor(private control: NgControl,
              private cdr: ChangeDetectorRef,
              @Host() @Optional() private mask: MaskDirective) {
  }

  ngOnInit(): void {
    this.mask.maskExpression = this.currentMask;
    this.mask.ngOnChanges({"maskExpression": new SimpleChange("", this.currentMask, true)});
    this.mask.registerOnChange((telefone) => {
      if (telefone != null && this.currentValue !== telefone) {
        this.currentValue = telefone;
        this.control.control.setValue(this.currentValue);
        this.processInputChange(telefone);
      }
    });
    this.cdr.detectChanges();
  }

  private processInputChange(telefone: string) {
    if (telefone.length <= 10) {
      if (this.currentMask != this.mask10) {
        this.currentMask = this.mask10;
        setTimeout(() => {
          this.mask.ngOnChanges({"maskExpression": new SimpleChange(null, this.mask10, false)});
        }, 50);
      }
    } else {
      this.currentMask = this.mask11;
      setTimeout(() => {
        this.mask.ngOnChanges({"maskExpression": new SimpleChange(null, this.mask11, false)});
      }, 50);

    }
  }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Type in the Middle of Letters Without the Next Letters ...
Disabling Overtype Mode in Windows. To stop overwriting the next character whenever you type a letter, press the "Insert" key on your keyboard....
Read more >
Type over text in Word for Windows - Microsoft Support
Type over text in Word for Windows ... The Insert key on your keyboard allows you to replace text as you type. You...
Read more >
Change HTML Textbox: Overwrite Instead of Insert as User ...
I am working on a service which will allow editing of text. To aid the user in the process, I'd like to allow...
Read more >
TI 83/84: Delete Mode vs. Insert Mode
press 2ND DEL. The cursor will change style; now it's in “insert mode” (INS). Type the missing “1”, and the other characters will...
Read more >
How to change alignment in Excel, justify, distribute and fill cells
Horizontal alignment · Align Left Align Left - aligns the contents along the left edge of the cell. · Center Center - puts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found