当前位置:网站首页>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, ''))
边栏推荐
- Codeforces Round #801 (Div. 2)A~C
- Sword finger offer II 019 Delete at most one character to get a palindrome
- Interesting drink
- Information security - Analysis of security orchestration automation and response (soar) technology
- Truck History
- Opencv learning log 27 -- chip positioning
- 2078. Two houses with different colors and the farthest distance
- [exercise-5] (UVA 839) not so mobile (balance)
- Codeforces Round #803 (Div. 2)A~C
- Understand what is a programming language in a popular way
猜你喜欢
Web based photo digital printing website
window11 conda安装pytorch过程中遇到的一些问题
Determine the Photo Position
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
Sword finger offer II 019 Delete at most one character to get a palindrome
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
【练习-5】(Uva 839)Not so Mobile(天平)
Information security - threat detection engine - common rule engine base performance comparison
QT按钮点击切换QLineEdit焦点(含代码)
Maximum product (greedy)
随机推荐
Codeforces Round #802(Div. 2)A~D
628. Maximum product of three numbers
Interesting drink
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
分享一个在树莓派运行dash应用的实例。
Data storage in memory & loading into memory to make the program run
C language must memorize code Encyclopedia
Read and save zarr files
Determine the Photo Position
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
AcWing:第58场周赛
Socket communication
1005. Maximized array sum after K negations
力扣——第298场周赛
滲透測試 ( 1 ) --- 必備 工具、導航
[exercise -10] unread messages
807. Maintain the urban skyline
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
1689. Ten - the minimum number of binary numbers
渗透测试 ( 4 ) --- Meterpreter 命令详解