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.

Mask for ion-input

See original GitHub issue

Feature Request

Masks for inputs on Ionic v4

Ionic Info

Ionic:

   ionic (Ionic CLI)          : 4.1.1 (/usr/local/lib/node_modules/ionic)
   Ionic Framework            : @ionic/angular 4.0.0-beta.3
   @angular-devkit/core       : 0.7.0-rc.3
   @angular-devkit/schematics : 0.7.0-rc.3
   @angular/cli               : 6.0.8
   @ionic/ng-toolkit          : 1.0.0
   @ionic/schematics-angular  : 1.0.1

System:

   NodeJS : v9.1.0 (/usr/local/bin/node)
   npm    : 5.5.1
   OS     : Linux 4.4

Describe the Feature Request Being able to natively add masks to ion-input, a pretty standard feature for most applications.

Describe Preferred Solution

<ion-input mask="(999) 999-9999" mask-placeholder="_"></ion-input>

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:91
  • Comments:33 (9 by maintainers)

github_iconTop GitHub Comments

15reactions
clmntrcommented, Nov 9, 2019

Based on @adamduren solution, here is a more generic directive :

// Angular
import { Directive, Input } from '@angular/core';

// Ionic
import { IonInput } from '@ionic/angular';

// Rxjs
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';

// Text-mask
import { createTextMaskInputElement } from 'text-mask-core';

/**
 * ion-mask directive, based on text-mask module
 */
@Directive({
  selector: '[ionMask]',
  providers: [IonInput],
})
export class IonMask {

  @Input('ionMask') 
  private mask            : Array<any>    = [];
  private onDestroy       : Subject<void> = new Subject<void>();

  constructor(public ionInput: IonInput) {}

  public ngOnInit() {
    this.configureInput();
  }

  public ngOnDestroy() {
    this.onDestroy.next();
  }

  public async configureInput() {
    const input       = await this.ionInput.getInputElement();
    const maskedInput = createTextMaskInputElement({
      inputElement  : input,
      mask          : this.mask,
      guide         : false
    });
    this.ionInput
      .ionChange
      .pipe( takeUntil( this.onDestroy ) )
      .subscribe( ( event: CustomEvent ) => {
        const { value } = event.detail;
        maskedInput.update(value);
        this.ionInput.value = input.value;
      });
  }

}

Use it like that in your component’s template :

<ion-input formControlName="controlName" [ionMask]="mask"></ion-input>

And in your component’s controller :

public mask : Array<any>  = [ 'y','o', 'u', 'r', ' ', 'm', 'a', 's', 'k' ]
15reactions
mateusduraescommented, Sep 5, 2018

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

@thiagoprz/ionic-input-mask - npm
Mask for ion-input. Latest version: 0.0.5, last published: 24 days ago. Start using @thiagoprz/ionic-input-mask in your project by running ...
Read more >
Need a Mask for ion-input ionic v4
I'm trying to use br-mask with the settings above. Normal bass via npm: npm install br-mask --save -E, then just insert the module...
Read more >
Ionic Input Mask (forked) - StackBlitz
Starter project for Ionic apps that exports to the Ionic CLI.
Read more >
Ionic - Format (use mask) input as currency - YouTube
Learn how to format numbers as currency for an Ionic / Angular project. To mask an input as currency is very easy with...
Read more >
Ionic 3 input mask - angular - Stack Overflow
step1 ; Directive, Attribute } from ; import { NgModel } from ; Directive({ selector: '[mask]' ...
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