简体   繁体   English

在角形材料2输入字段中仅允许数字TAB,DELETE,BACKSPACE,向左箭头,向右箭头

[英]allow only numbers, TAB , DELETE, BACKSPACE, Left Arrow, Right Arrow in angular material 2 input field

I'm trying to block chars and other keyboard inputs, except Numbers , Tab Key , Backspace , Delete , Left Arrow , Right Arrow 我正在尝试阻止字符和其他键盘输入,但NumbersTab键BackspaceDeleteLeft ArrowRight Arrow除外

I just tried following code, but this can type few characters, 我刚刚尝试了以下代码,但这可以键入几个字符,

<input type="text" matInput required name="mobileNumber"  onkeypress='return ((event.charCode >= 48 && event.charCode <= 57) || (event.charCode >= 96 && event.charCode <= 105) || (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39))'>

this is the working example 这是工作示例

How can I block those properly 我该如何正确阻止

Below is my solution: 下面是我的解决方案:

import { Directive, ElementRef, HostListener } from '@angular/core';

@Directive({
    selector: '[myNumberOnly]'
})
export class NumberOnlyDirective {
    // Allow decimal numbers. The \. is only allowed once to occur
    private regex: RegExp = new RegExp(/^[0-9]+(\.[0-9]*){0,1}$/g);

    // Allow key codes for special events. Reflect :
    // Backspace, tab, end, home
    private specialKeys: Array<string> = [ 'Backspace', 'Tab', 'End', 'Home' ];

    constructor(private el: ElementRef) {
    }

    @HostListener('keydown', [ '$event' ])
    onKeyDown(event: KeyboardEvent) {
        // Allow Backspace, tab, end, and home keys
        if (this.specialKeys.indexOf(event.key) !== -1) {
            return;
        }

        // Do not use event.keycode this is deprecated.
        // See: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
        let current: string = this.el.nativeElement.value;
        // We need this because the current value on the DOM element
        // is not yet updated with the value from this event
        let next: string = current.concat(event.key);
        if (next && !String(next).match(this.regex)) {
            event.preventDefault();
        }
    }
}

I'm pretty sure you're wondering what's the issue here, since the char codes you're using are HTML Char Codes and you are definetly using the right ones. 我敢肯定,您想知道这里是什么问题,因为您使用的字符代码是HTML字符代码,并且您肯定使用了正确的字符代码。 The problem here is that your onkeypress event gives you the ASCII code of the characters, that's why you can type from 'a' to 'i' in your input field. 这里的问题是您的onkeypress事件为您提供了字符的ASCII码,这就是为什么您可以在输入字段中输入从'a''i'

What I will suggest is to just leave it like this 我会建议就是这样离开

onkeypress='return (event.charCode >= 48 && event.charCode <= 57)'

The other keys (arrows, delete, etc..) will still work. 其他键(箭头,删除等)仍将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Angular 9 中使用向上、向下、向左和向右箭头键导航表内的动态输入和选择字段? - How to Navigate the dynamic input and select field inside the table with UP, DOWN, LEFT and RIGHT arrow keys in Angular 9? 在“角度材质”选项卡后添加箭头css - Add arrow css after angular material tab 角度材质选项卡活动链接放置下拉箭头 - Angular Material Tab active link to put drop down arrow Angular 使用左右箭头键在表格单元格之间移动 - Angular use left right arrow key to move between table cells 如何在按下左右箭头键时将箭头键焦点设置在这些按钮上? - How do i set arrow key focus on these button in angular while pressing the left and right arrow keys? 将左右箭头键绑定到 <mat-tab-group> 面板,实现轮播 - Bind right/left arrow keys to <mat-tab-group> panels, to implement Carousel 如何将 Angular Material 扩展面板箭头图标定位在左侧 - How to position the Angular Material expansion panel arrow icon on the left-hand side 向角材料工具提示箭头添加阴影 - Adding shadow to angular material tooltip arrow 使用 matTooltip angular 材质的工具提示设置箭头 - set arrow with tooltip of matTooltip angular material 标题中的 Angular Material 2 数据表排序箭头 - Angular Material 2 data table sorting arrow in header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM