Monday, February 12, 2018

Input Only Number in Iput Type in Angular 2

Input Only Number in Input Type in Angular 2 -- Html Code here First Create Directive.ts File and Put Code Inside *************************onlynumber.directive.ts*************************** import { Directive, ElementRef, HostListener, Input } from '@angular/core'; @Directive({ selector: '[OnlyNumber]' }) export class OnlyNumber { constructor(private el: ElementRef) { } @Input() OnlyNumber: boolean; @HostListener('keydown', ['$event']) onKeyDown(event) { let e = event; if (e.which == 13) { e.preventDefault(); var $next = $('[tabIndex=' + (+ parseInt(e.target['tabIndex']) + 1) + ']'); if (!$next.length) { $next = $('[tabIndex=1]'); } $next.focus(); } else if (this.OnlyNumber) { if ([46, 8, 9, 27, 13, 190].indexOf(e.keyCode) !== -1 || (e.keyCode == 65 && e.ctrlKey === true) || (e.keyCode == 67 && e.ctrlKey === true) || (e.keyCode == 88 && e.ctrlKey === true) || (e.keyCode >= 35 && e.keyCode <= 39)) { return; } if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { e.preventDefault(); } } } }

No comments: