当前位置:网站首页>input 只能输入数字,限定输入
input 只能输入数字,限定输入
2022-07-06 09:28:00 【梦想身高1米8】
string.replace(/[^\d]/g, ''),把字符串内非数字的字符替换成空字符/[^\d]/g是正则表达式,全局匹配非数字字符
1 onkeyup事件
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; const custom = () => {
return <input placeholder="请输入" onKeyUp={
e => e.target.value = e.target.value.replace(/[^\d]/g, '')} /> } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>

如果把onkeyup换成onkeydown或onkeypress,是达不到效果的,虽然onkeyup的效果也不是很好
2 onchange配合value
onkeyup事件本质上就是在键盘松开的时候改变input内的值,那直接用onchange和value不就可以了。
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; const custom = () => {
const [number, setNumber] = React.useState() return <input placeholder="请输入" value={
number} onChange={
e => setNumber(e.target.value.replace(/[^\d]/g, ''))} /> } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>
动图上可能看不出来,其实我在输入’2’之后疯狂摁字母键
3 限定输入规则(正则表达)
只能输入数字string.replace(/[\D]/g, ''))
只能输入数字、英文string.replace(/[^A-Za-z0-9]/g, ''))
只能输入数字、英文、下斜杠string.replace(/[\W]/g, ''))
只能输入数字、小数点(不是小数,可以有多个小数点)string.replace(/[^0-9.]/g, ''))
边栏推荐
- Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
- Write web games in C language
- Essai de pénétration (1) - - outils nécessaires, navigation
- [teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
- 双向链表—全部操作
- Web based photo digital printing website
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- Alice and Bob (2021 Niuke summer multi school training camp 1)
- Pyside6 signal, slot
猜你喜欢

TCP's three handshakes and four waves
frida hook so层、protobuf 数据解析

QT实现圆角窗口

Penetration test (1) -- necessary tools, navigation

力扣:第81场双周赛

921. Minimum additions to make parentheses valid

读取和保存zarr文件

1013. Divide the array into three parts equal to and

1605. Sum the feasible matrix for a given row and column
![MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’](/img/e6/f4a696179282fe1f4193410c5a493a.png)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
随机推荐
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
【练习-9】Zombie’s Treasure Chest
C language is the watershed between low-level and high-level
QWidget代码设置样式表探讨
快速转 TypeScript 指南
What is the difficulty of programming?
[exercise -10] unread messages
力扣:第81场双周赛
Socket communication
Openwrt build Hello ipk
TCP's three handshakes and four waves
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Web based photo digital printing website
Borg maze (bfs+ minimum spanning tree) (problem solving report)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
E. Breaking the Wall
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
Opencv learning log 27 -- chip positioning
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
The most complete programming language online API document