The inputmask plugin does the best job of this. Its extremely flexible in that you can supply whatever regex you like to restrict input. It also does not require JQuery.
Step 1: Install the plugin:
npm install --save inputmask
Step2: create a directive to wrap the input mask:
import {Directive, ElementRef, Input} from '@angular/core'; import * as Inputmask from 'inputmask'; @Directive({ selector: '[app-restrict-input]', }) export class RestrictInputDirective { // map of some of the regex strings I'm using (TODO: add your own) private regexMap = { integer: '^[0-9]*$', float: '^[+-]?([0-9]*[.])?[0-9]+$', words: '([A-z]*\\s)*', point25: '^\-?[0-9]*(?:\\.25|\\.50|\\.75|)$' }; constructor(private el: ElementRef) {} @Input('app-restrict-input') public set defineInputType(type: string) { Inputmask({regex: this.regexMap[type], placeholder: ''}) .mask(this.el.nativeElement); } }
Step 3:
<input type="text" app-restrict-input="integer">
Check out their github docs for more information.