Angular 指令 - 只有数字和 2 位小数,用逗号分隔
Angular directive - Only digits and 2 decimals with comma separated
我只需要允许输入一个数字和可选的 1 个逗号和 2 个小数。
正确输入示例:
123
123,22
我试过这个在网上找到的指令,但它允许我输入这些字符:`、´、+
指令代码:
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[appTwoDigitDecimaNumber]'
})
export class TwoDigitDecimalNumberDirective {
// Allow decimal numbers and negative values
private regex: RegExp = new RegExp(/^\d*\,?\d{0,2}$/g);
// Allow key codes for special events. Reflect :
// Backspace, tab, end, home
private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', 'ArrowLeft', 'ArrowRight', 'Del', 'Delete'];
constructor(private el: ElementRef) {
}
@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
console.log(this.el.nativeElement.value);
// Allow Backspace, tab, end, and home keys
if (this.specialKeys.indexOf(event.key) !== -1) {
return;
}
let current: string = this.el.nativeElement.value;
const position = this.el.nativeElement.selectionStart;
const next: string = [current.slice(0, position), event.key == 'Decimal' ? ',' : event.key, current.slice(position)].join('');
if (next && !String(next).match(this.regex)) {
event.preventDefault();
}
}
}
这段代码有什么问题?
谢谢
只需将您的输入类型数字更改为文本
管道:
import { Directive, HostListener, ElementRef } from '@angular/core';
@Directive({
selector: '[appDecimalAmount]'
})
export class DecimalAmountDirective {
private regex: RegExp = new RegExp(/^\d*\,?\d{0,2}$/g);
private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', 'ArrowLeft', 'ArrowRight', 'Del', 'Delete'];
constructor(private el: ElementRef) { }
@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
if (this.specialKeys.indexOf(event.key) !== -1) {
return;
}
const current: string = this.el.nativeElement.value;
const next: string = current.concat(event.key);
if (next && !String(next).match(this.regex)) {
event.preventDefault();
}
}
}
HTML
<label> Cost (€) <sup class="text-danger">*</sup></label>
<input type="text" formControlName="predictShippingCost" min="0" appDecimalAmount>
``
我只需要允许输入一个数字和可选的 1 个逗号和 2 个小数。
正确输入示例:
123
123,22
我试过这个在网上找到的指令,但它允许我输入这些字符:`、´、+
指令代码:
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[appTwoDigitDecimaNumber]'
})
export class TwoDigitDecimalNumberDirective {
// Allow decimal numbers and negative values
private regex: RegExp = new RegExp(/^\d*\,?\d{0,2}$/g);
// Allow key codes for special events. Reflect :
// Backspace, tab, end, home
private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', 'ArrowLeft', 'ArrowRight', 'Del', 'Delete'];
constructor(private el: ElementRef) {
}
@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
console.log(this.el.nativeElement.value);
// Allow Backspace, tab, end, and home keys
if (this.specialKeys.indexOf(event.key) !== -1) {
return;
}
let current: string = this.el.nativeElement.value;
const position = this.el.nativeElement.selectionStart;
const next: string = [current.slice(0, position), event.key == 'Decimal' ? ',' : event.key, current.slice(position)].join('');
if (next && !String(next).match(this.regex)) {
event.preventDefault();
}
}
}
这段代码有什么问题?
谢谢
只需将您的输入类型数字更改为文本
管道:
import { Directive, HostListener, ElementRef } from '@angular/core';
@Directive({
selector: '[appDecimalAmount]'
})
export class DecimalAmountDirective {
private regex: RegExp = new RegExp(/^\d*\,?\d{0,2}$/g);
private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', 'ArrowLeft', 'ArrowRight', 'Del', 'Delete'];
constructor(private el: ElementRef) { }
@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
if (this.specialKeys.indexOf(event.key) !== -1) {
return;
}
const current: string = this.el.nativeElement.value;
const next: string = current.concat(event.key);
if (next && !String(next).match(this.regex)) {
event.preventDefault();
}
}
}
HTML
<label> Cost (€) <sup class="text-danger">*</sup></label>
<input type="text" formControlName="predictShippingCost" min="0" appDecimalAmount>
``