当前位置:网站首页>When the input tag type is number, the input of E, e, -, + is blocked
When the input tag type is number, the input of E, e, -, + is blocked
2022-06-25 13:21:00 【acgCode】
When we put the label <input> Of type Set to number when , You cannot enter a string other than a number , Only E,e,-,+ With the exception of .
So when you type E,e,-,+ In fact <input> What does the value of become ? I recorded the actual input at that moment .
<input id="input" type="number" />
document.getElementById('input').addEventListener("input", event => {
var value = event.target.value;
console.log(value, typeof value, value.length);
});
The output is :
As shown in figure shows , Is a length of 0 Empty string for .
Although here value Becomes a length of 0 Empty string for , The input characters are left in the input box . So how to prevent such invalid character input ? The solution is as follows :
document.getElementById('input').addEventListener("keydown", event => {
let {
key } = event;
event.returnValue = /[0-9\bBackspace\b]/.test(key);
});
Here we are listening <input> Of keydown event , When the keyboard is pressed , You can listen to which key is pressed . If it is not the number key and the back key, this input is not allowed . This will ensure that each input is a number .
边栏推荐
- 时间过滤器(el-table)中使用
- QT display ffmpeg decoded pictures
- The starting point for learning programming.
- [pit avoidance means "difficult"] antd select fuzzy search
- KDD 2022 | graphmae: self supervised mask map self encoder
- Qt鼠标跟踪
- Solution to Nacos' failure to modify the configuration file mysql8.0
- KDD 2022 | GraphMAE:自监督掩码图自编码器
- Sword finger offer day 2 linked list (simple)
- An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
猜你喜欢

Sword finger offer day 1 stack and queue (simple)

揭秘GaussDB(for Redis):全面对比Codis

數據在內存中的存儲相關內容

Sword finger offer II 029 Sorted circular linked list

An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down

Custom vertical table

Serenvlt first met

Sword finger offer day 3 string (simple)

三行代码简单修改jar包的项目代码

Of binary tree_ Huffman tree_ Huffman encoding
随机推荐
与生产环境中的 console.log 说再见
AGCO AI frontier promotion (6.25)
Introduction to mongodb chapter 01 introduction to mongodb
Cold migration and resize of Nova component source code analysis
关于扫雷的简易实现
Sword finger offer day 1 stack and queue (simple)
关于数据在内存中存储的相关例题
三行代码简单修改jar包的项目代码
Solve the problem that yarn cannot load files in vs Code
1024 hydrology
Back test of quantitative trading - tqzfuturerenkowavestrategy
Sword finger offer II 028 Flatten multi-level bidirectional linked list
Django framework - caching, signaling, cross site request forgery, cross domain issues, cookie session token
It's an artifact to launch a website in a few minutes
JVM parameter interpretation
一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推
Stockage des données en mémoire
Native JS --- infinite scrolling
Sword finger offer II 032 Effective anagrams
Nova中的api