/
numbers-only.directive.ts
71 lines (64 loc) · 2.25 KB
/
numbers-only.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Directive, HostListener, Input } from '@angular/core';
import { truthyAttr } from 'app/shared/helper';
import { FormatService } from 'app/core/format.service';
const ALLOWED = [
'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown',
'End', 'Home', 'Delete', 'Backspace', 'Tab',
'Shift', 'Control', 'Alt', 'Super', 'Meta',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
];
/**
* A directive to only allow digits to be entered by users
*/
@Directive({
selector: '[numbersOnly]'
})
export class NumbersOnlyDirective {
private enabled: boolean;
@Input() set numbersOnly(numbersOnly: boolean | string) {
this.enabled = truthyAttr(numbersOnly);
}
private _allowDecimalSeparator: boolean | string = false;
@Input() get allowDecimalSeparator(): boolean | string {
return this._allowDecimalSeparator;
}
set allowDecimalSeparator(allow: boolean | string) {
this._allowDecimalSeparator = truthyAttr(allow);
}
constructor(private format: FormatService) {
}
@HostListener('keydown', ['$event']) onKeyDown(event: KeyboardEvent) {
if (this.enabled) {
let allowed = !event.ctrlKey && !event.altKey;
if (allowed) {
if (this.allowDecimalSeparator && [',', '.'].includes(event.key)) {
// Manually insert the decimal separator
const input = event.target as HTMLInputElement;
const val = input.value;
const sep = this.format.decimalSeparator;
const selStart = input.selectionStart;
if (!val.includes(sep)) {
input.value = val.substring(0, selStart) + sep + val.substring(input.selectionEnd);
input.setSelectionRange(selStart + 1, selStart + 1, 'none');
input.dispatchEvent(new Event('input'));
}
allowed = false;
} else {
allowed = ALLOWED.includes(event.key);
}
}
if (!allowed) {
event.preventDefault();
}
}
}
@HostListener('paste', ['$event']) onPaste(event: ClipboardEvent) {
if (this.enabled) {
const data = event.clipboardData.getData('text/plain');
const regexp = this.allowDecimalSeparator ? new RegExp(`^[\\d\\${this.format.decimalSeparator}]+$`) : /^\d+$/;
if (!regexp.test(data)) {
event.preventDefault();
}
}
}
}