当前位置:网站首页>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, ''))
边栏推荐
- QT实现圆角窗口
- 1013. Divide the array into three parts equal to and
- D - function (HDU - 6546) girls' competition
- 树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
- TCP's three handshakes and four waves
- 【练习-5】(Uva 839)Not so Mobile(天平)
- Interval sum ----- discretization
- Opencv learning log 26 -- detect circular holes and mark them
- QNetworkAccessManager实现ftp功能总结
- 【练习-7】Crossword Answers
猜你喜欢
![[exercise-7] crossover answers](/img/66/3dcba2e70a4cd899fbd78ce4d5bea2.png)
[exercise-7] crossover answers

Analysis of protobuf format of real-time barrage and historical barrage at station B

628. Maximum product of three numbers

Programmers, what are your skills in code writing?

1005. Maximized array sum after K negations

树莓派4B安装opencv3.4.0

(POJ - 3579) median (two points)

树莓派4B64位系统安装miniconda(折腾了几天终于解决)

Flask框架配置loguru日志庫

Radar equipment (greedy)
随机推荐
(POJ - 2739) sum of constructive prime numbers (ruler or two points)
C language must memorize code Encyclopedia
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
807. Maintain the urban skyline
Essai de pénétration (1) - - outils nécessaires, navigation
树莓派4B安装opencv3.4.0
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
Penetration test (3) -- Metasploit framework (MSF)
Nodejs crawler
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
Frida hook so layer, protobuf data analysis
分享一个在树莓派运行dash应用的实例。
Shell Scripting
Alice and Bob (2021 Niuke summer multi school training camp 1)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
AcWing:第56场周赛
Common configuration files of SSM framework
Ball Dropping
D - function (HDU - 6546) girls' competition
1529. Minimum number of suffix flips