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.

Angular2, inputmask ngModel binding

See original GitHub issue

Hello everyone, I have some problem with ng2 ang inputmask. For example I have had this code component.html <div class="form-group col-sm-7"> <label class="control-label" for="sender-phone">Phone *</label> <input type="text" [(ngModel)]="sender.phone" class="form-control" id="sender-phone" placeholder="Phone"> </div> component.ts ngAfterViewInit() { let phoneNumberInput = document.getElementById('sender-phone'); let inputmask = new Inputmask('+7(999)999-99-99'); inputmask.mask(element); }

Inputmask is working good. But my [(ngModel)] didn’t work. Anyone know how I can resolve my issue ?

My code for example http://plnkr.co/edit/F3pob7hH2ZrJv0MDNa9x?p=preview

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
gregmagolancommented, Nov 9, 2017

And this is a similar directive for restrict input:

import {Directive, ElementRef, Input, Optional} from '@angular/core';
import {NgControl} from '@angular/forms';

@Directive({
  selector: '[restrictInput]',
})
export class RestrictInputDirective {
  private regexMap: { [key: string]: string } = {
    'integer': '^[+-]?[0-9]*$',
    'positive-integer': '^[0-9]*$',
    'float': '^[+-]?([0-9]*[.])?[0-9]+$',
    'positive-float': '^([0-9]*[.])?[0-9]+$',
    'amount': '^[+-]?([0-9]*[.])?[0-9]?[0-9]?$',
    'positive-amount': '^([0-9]*[.])?[0-9]?[0-9]?$',
    'words': '([A-z]*\\s)*',
  };

  @Input('restrictInput')
  mask: any;

  @Input('restrictInputOptions')
  options: any;

  constructor(
    private elementRef: ElementRef,
    @Optional() private control: NgControl,
  ) {}

  ngOnChanges(): void {
    if (!this.control) {
      console.warn('No control for RestrictInputDirective');
      return;
    }

    if (this.mask) {
      let options: any = _.clone(this.options) || {};
      _.defaults(options, { showMaskOnHover: false, placeholder: '' });
      options.regex = this.regexMap[this.mask] || this.mask;
      options.oncomplete = options.onincomplete = options.oncleared = () => this.handleChange();
      Inputmask(options).mask(this.elementRef.nativeElement);
    } else {
      Inputmask.remove(this.elementRef.nativeElement);
    }
  }

  private handleChange(): void {
    this.control.control.patchValue(this.elementRef.nativeElement.value);
  }
}

Usage like so:

<input formControlName="cost" placeholder="Cost" restrictInput="positive-amount">
2reactions
qwdingcommented, Jul 18, 2016

I use inputmask through primeng(http://www.primefaces.org/primeng/#/mask). I decided to use it later until it is fixed. thx~

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular2, inputmask ngModel binding - Stack Overflow
I have resolve problem of this issue. http://plnkr.co/edit/tN6y5d2fsl0b7cRq6Prv?p=preview. You need create directive with InputMask
Read more >
Angular2, inputmask ngModel binding - Google Groups
The problem is that your JQ plugin is not fireing the needed events. It eats the change and input events. It does not...
Read more >
jQuery inputmask plugin + AngularJS - valve's
I was working on a small angularjs application and needed a text input masking to force users to enter their telephone numbers in...
Read more >
How to Use Input Masks to Validate Input in an Angular App
Input validation is always a chore to set up. An input mask is a way to enforce the format of the user's input...
Read more >
InputMask Control | Input | Angular Wijmo Demos - GrapeCity
The InputMask control allows you to validate and format user input as it is entered, preventing users from entering invalid data.
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