不让输入特殊字符

onKeyDown(e: KeyboardEvent): void {
console.log(e);
if (e.keyCode === 187 || e.keyCode === 190) {
event.preventDefault();
event.stopPropagation();
}
}

只运行input输入数字,并且不能出现09的显示,可以如此做:
numberKeyDown(value: number, e: KeyboardEvent): void {
let str = '';
if (value != null) { str = value.toString(); }

    if (str === '0' && ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105))) {// 0->09
        e.target['value'] = '';
    }

    if (e.keyCode === 61 || e.keyCode === 187 || e.keyCode === 107 || e.keyCode === 190 || e.keyCode === 110) {// '+' and '.'
        e.preventDefault();
        e.stopPropagation();
    }
}

使用:
(keydown)="numberKeyDown(data.Ip.Ipv4.TTL,$event)" [class.input-error]="data.Ip.Ipv4.TTL > 255 || data.Ip.Ipv4.TTL < 0 )"
style="text-align:center; background:transparent; width:100%" min="0" max="255" [(ngModel)]="data.Ip.Ipv4.TTL" />

注意:
onKey(event: KeyboardEvent) { // with type info
this.values += (event.target).value + ' | ';//这里可以直接得到字符串
}

你可能感兴趣的:(不让输入特殊字符)