フィールドに数字のみを入力するように制限する必要があります。 私は解決策がありますが、入力フィールドでバックスペースが機能していません。 誰にでも適切な解決策がありますか?入力テキストボックスを制限する方法は、数値をAngular2形式で入力するだけですか?
<input (keypress)="keyPress($event)" minlength="8" maxlength="15" required />
keyPress(event: any) {
const pattern = /[0-9\+\-\ ]/;
let inputChar = String.fromCharCode(event.charCode);
// console.log(inputChar, e.charCode);
if (!pattern.test(inputChar)) {
// invalid character, prevent input
event.preventDefault();
}
}
kepressイベントでは、それは数字のみを入力するようにユーザを制限するが、このコードの問題は、バックスペースで、タブキーはありませんこのコードは私の期待どおりではありません...
可能な複製:http://stackoverflow.com/questions/7295843/allow-only-numbers-to-be-typed-in-a-textbox –
[数値(0-9)のみを許可する方法] HTML入力ボックスのjQueryを使用して?](http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery) – Igor
私はjQueryを使用していません。任意のサービスを作成することによってtypescript-angular2に解決策がありますか。 – RohanArihant