当前位置:网站首页>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>
边栏推荐
- Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
- Fundamentals - configuration file analysis
- 信息学奥赛一本通 1339:【例3-4】求后序遍历 | 洛谷 P1827 [USACO3.4] 美国血统 American Heritage
- Ffplay document [easy to understand]
- Oracle-表空间管理
- IC科普文:ECO的那些事儿
- 信息学奥赛一本通 1340:【例3-5】扩展二叉树
- 无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
- 信息学奥赛一本通 1338:【例3-3】医院设置 | 洛谷 P1364 医院设置
- About the priority of Bram IP reset
猜你喜欢
Applet event binding
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
2.8、项目管理过程基础知识
Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
[record of question brushing] 1 Sum of two numbers
2.8 basic knowledge of project management process
CTF reverse Foundation
14、Transformer--VIT TNT BETR
IC科普文:ECO的那些事儿
随机推荐
ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
Schema和Model
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
Model method
Composition of applet code
mongodb文档间关系
[C language] merge sort
Is it safe for CICC fortune to open an account online?
证券开户选择哪个证券比较好?网上开户安全么?
CCPC 2021 Weihai - G. shinyruo and KFC (combination number, tips)
1、强化学习基础知识点
Unity编辑器扩展 UI控件篇
Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)
E. Singhal and numbers (prime factor decomposition)
信息学奥赛一本通 1339:【例3-4】求后序遍历 | 洛谷 P1827 [USACO3.4] 美国血统 American Heritage
Informatics Orsay all in one 1339: [example 3-4] find the post order traversal | Valley p1827 [usaco3.4] American Heritage
CVPR 2022 | common 3D damage and data enhancement
Mongodb/ document operation
Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design