当前位置:网站首页>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, ''))
边栏推荐
- 【练习-6】(Uva 725)Division(除法)== 暴力
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
- JS call camera
- Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
- Codeforces Round #803 (Div. 2)A~C
- 栈的经典应用—括号匹配问题
- New to redis
- C language is the watershed between low-level and high-level
- Information security - security professional name | CVE | rce | POC | Vul | 0day
- 1013. Divide the array into three parts equal to and
猜你喜欢
[exercise-7] crossover answers
C language learning notes
Maximum product (greedy)
Penetration test (4) -- detailed explanation of meterpreter command
第 300 场周赛 - 力扣(LeetCode)
快速转 TypeScript 指南
Gartner: five suggestions on best practices for zero trust network access
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
树莓派4B64位系统安装miniconda(折腾了几天终于解决)
Openwrt build Hello ipk
随机推荐
第 300 场周赛 - 力扣(LeetCode)
[exercise 4-1] cake distribution
Socket communication
C language must memorize code Encyclopedia
Frida hook so layer, protobuf data analysis
Configuration du cadre flask loguru log Library
滲透測試 ( 1 ) --- 必備 工具、導航
(POJ - 3685) matrix (two sets and two parts)
1855. Maximum distance of subscript alignment
【练习-8】(Uva 246)10-20-30==模拟
【练习-2】(Uva 712) S-Trees (S树)
读取和保存zarr文件
Codeforces Round #803 (Div. 2)A~C
409. Longest palindrome
图图的学习笔记-进程
C basic grammar
[exercise-8] (UVA 246) 10-20-30== simulation
Information security - security professional name | CVE | rce | POC | Vul | 0day
Some problems encountered in installing pytorch in windows11 CONDA
Opencv learning log 28 -- detect the red cup cover