当前位置:网站首页>Use of form text box (II) input filtering (synthetic event)
Use of form text box (II) input filtering (synthetic event)
2022-07-05 20:33:00 【Red blue purple】
Use of form text boxes ( Two ) The input filter ( Composite event )
The input filter
Shielding character
scene : The input box needs to limit the characters that appear , For example, it can only be numbers .
The input box itself does not have this function , But we can pass JavaScript To achieve . We can input characters into the input box , Rely on keyboard events , So you can add keyboard events , Then, according to the information of the event object, it is judged that the operator does not meet the conditions , disqualification , Just through event.preventDefault
Block default events , Block input .
<body>
<div class="input-box">
<input type="text" size="10" maxlength="10">
</div>
<script>
const ipt = document.getElementsByTagName('input')[0]
ipt.addEventListener('keypress', (e) => {
console.log(e.key)
if (!/^\d/.test(e.key)) {
e.preventDefault()
}
})
</script>
</body>
The keyboard event we added above is keypress
, because keyup
It is a keyboard lift event , At this time, it has been entered into the input box , It doesn't work ; and keydown
Recognize function keys , Therefore, the function keys must be allowed to pass , Otherwise, there is no way to delete the input . in addition ,keypress
Support case sensitivity .

Processing shear plates
We have realized that only numbers can be entered , But if we copy non numeric data from outside , Pasting into the text box will break through our input filter .

At this time, we need to strengthen our input filtering through the clipboard event (HTML5 Added a clipboard event )
copy
: Triggered when a copy operation occurscut
: Triggered when the cut operation occurspaste
: Triggered when the paste operation occurs
These three events are prefixed before
The version is triggered before the operation of , But not often , I don't know any specific use situations . Blocking events can only be blocked in the three events triggered when they occur .
How to get the data of the shear board ? Can pass event
On the object clipboardData
Object to get , To prevent unauthorized access to the clipboard , Can only be accessed during a clipboard event clipboardData
object .
clipboardData
There are 3 A way :getDate
、setData
、clearData
.
const ipt = document.getElementsByTagName('input')[0]
ipt.addEventListener('keypress', (e) => {
console.log(e.key)
if (!/^\d/.test(e.key)) {
e.preventDefault()
}
})
ipt.addEventListener('copy', (e) => {
// e.clipboardData.setData The first parameter is the format , The second parameter is the copied content
e.clipboardData.setData('text/plain', ' Duplicate false data ')
// Mask out default events , Copy fake data
e.preventDefault()
})
ipt.addEventListener('paste', (e) => {
// Read the data of the shear board
const text = e.clipboardData.getData('text/plain')
// Do not paste if the conditions are not met
if (!/^\d/.test(text)) {
e.preventDefault()
}
})
It looks like , Even the pasted data can only be input into the input box if it is a number .

Dealing with Chinese 、 Japanese and other input methods

When we use input methods , It will still bypass our limit of only entering numbers .
Here is an interesting knowledge point Composite event
In Chinese, you need to press multiple keys at the same time to input a character . Synthetic events are used to detect and control this input , The input character is in the... Of the event object data
in .
compositionstart
: Indicates that input is about to begin , heredata
For the empty stringcompositionupdate
: Triggered when a new character is inserted , heredata
Characters entered forcompositionend
: Indicates that normal keyboard input is about to be restored , heredata
For the text to be entered into the input box
practice :
const ipt = document.getElementsByTagName('input')[0]
ipt.addEventListener('compositionstart', (e) => {
console.log('%c%s', 'color: red;font-size: 16px;', 'conpositionstart')
console.log(e.data)
})
ipt.addEventListener('compositionupdate', (e) => {
console.log('%c%s', 'color: blue;font-size: 16px;', 'compositionupdate')
console.log(e.data)
})
ipt.addEventListener('compositionend', (e) => {
console.log('%c%s', 'color: purple;font-size: 16px;', 'compositionend')
})

So we can at the end of the composite event , namely compositionend
In the event handler of , Remove the input Chinese , It is not allowed to input Chinese characters .
const ipt = document.getElementsByTagName('input')[0]
ipt.addEventListener('compositionend', (e) => {
// Remove the last few digits
e.target.value = e.target.value.slice(0, -e.data.length)
})

Complete code
<body>
<div class="input-box">
<input type="text" size="10" maxlength="10">
</div>
<script>
const ipt = document.getElementsByTagName('input')[0]
ipt.addEventListener('keypress', (e) => {
console.log(e.key)
if (!/^\d/.test(e.key)) {
e.preventDefault()
}
})
ipt.addEventListener('paste', (e) => {
// Read the data of the shear board
const text = e.clipboardData.getData('text/plain')
// Do not paste if the conditions are not met
if (!/^\d/.test(text)) {
e.preventDefault()
}
})
ipt.addEventListener('compositionend', (e) => {
// Remove the last few digits
e.target.value = e.target.value.slice(0, -e.data.length)
})
</script>
</body>
边栏推荐
- Relationship between mongodb documents
- 14、Transformer--VIT TNT BETR
- .Net分布式事务及落地解决方案
- mongodb文档间关系
- Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
- 本季度干货导航 | 2022年Q2
- 2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
- 【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
- Zero cloud new UI design
- National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
猜你喜欢
Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
全国爱眼教育大会,2022第四届北京国际青少年眼健康产业展会
【数字IC验证快速入门】3、数字IC设计全流程介绍
How to form standard interface documents
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
Zero cloud new UI design
Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
Leetcode brush question: binary tree 13 (the same tree)
随机推荐
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
js方法传Long类型id值时会出现精确损失
小程序全局配置
mongodb基操的练习
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
A way to calculate LNX
Mysql频繁操作出现锁表问题
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
【数字IC验证快速入门】6、Questasim 快速上手使用(以全加器设计与验证为例)
Usaco3.4 "broken Gong rock" band raucous rockers - DP
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
CTF逆向基础
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
小程序事件绑定
Simple understanding of interpolation search
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
Unity编辑器扩展 UI控件篇
Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
Practical demonstration: how can the production research team efficiently build the requirements workflow?
[record of question brushing] 1 Sum of two numbers