首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅数字验证在ionic应用程序中不起作用

仅数字验证在ionic应用程序中不起作用
EN

Stack Overflow用户
提问于 2021-04-28 15:12:05
回答 1查看 46关注 0票数 0
代码语言:javascript
复制
      <input type="tel" (keydown)="numberOnlyValidation($event)">

//函数无法读取输入数值

//在输入字段中键入时,没有显示任何内容,主要无法读取键盘数值

代码语言:javascript
复制
numberOnlyValidation(event: any) {
        console.log(event.target.value)

//pattern 

        const pattern = /[0-9]/;
        const inputChar = String.fromCharCode(event.charCode);
       

        if (!pattern.test(inputChar)) {
            // invalid character, prevent input
            event.preventDefault();
        }
    }

``
EN

回答 1

Stack Overflow用户

发布于 2021-04-28 15:59:47

.charCode属性仅存在于keypress事件Keyboard Event (developer.mozilla.org)

The specification says to use the KeyboardEvent.key property instead (developer.mozilla.org)

除了处理delete之外,如果您将此行更改为使用event.key,您的代码也应该可以工作

代码语言:javascript
复制
const inputChar = String.fromCharCode(event.charCode); // remove
const inputChar = event.key; // add
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67295452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档