当前位置:网站首页>Input can only input numbers, limited input
Input can only input numbers, limited input
2022-07-06 16:14:00 【Dream height 1.8 meters】
string.replace(/[^\d]/g, ''), Replace non numeric characters in the string with empty characters/[^\d]/gIt's a regular expression , Match non numeric characters globally
1 onkeyup event
<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=" Please enter " onKeyUp={
e => e.target.value = e.target.value.replace(/[^\d]/g, '')} /> } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>

If you put onkeyup Switch to onkeydown or onkeypress, It doesn't work , although onkeyup The effect of is not very good 
2 onchange coordination value
onkeyup The essence of the event is to change when the keyboard is released input Internal value , The direct use onchange and value Not on it .
<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=" Please enter " value={
number} onChange={
e => setNumber(e.target.value.replace(/[^\d]/g, ''))} /> } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>
You may not see it on the moving picture , Actually, I'm typing ’2’ Then press the letter key crazily 
3 Restrict input rules ( Regular expression )
You can only enter Numbers string.replace(/[\D]/g, ''))
You can only enter Numbers 、 english string.replace(/[^A-Za-z0-9]/g, ''))
You can only enter Numbers 、 english 、 Down slash string.replace(/[\W]/g, ''))
You can only enter Numbers 、 decimal point ( It's not a decimal , There can be multiple decimal points )string.replace(/[^0-9.]/g, ''))
边栏推荐
猜你喜欢
frida hook so层、protobuf 数据解析

Nodejs+vue online fresh flower shop sales information system express+mysql

QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系

1855. Maximum distance of subscript alignment
Quick to typescript Guide

Basic Q & A of introductory C language

921. Minimum additions to make parentheses valid
Frida hook so layer, protobuf data analysis

807. Maintain the urban skyline

第 300 场周赛 - 力扣(LeetCode)
随机推荐
Auto.js入门
The concept of C language array
双向链表—全部操作
Shell Scripting
Ball Dropping
分享一个在树莓派运行dash应用的实例。
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
b站 实时弹幕和历史弹幕 Protobuf 格式解析
Perform general operations on iptables
1323. Maximum number of 6 and 9
Penetration test (1) -- necessary tools, navigation
1005. Maximized array sum after K negations
Nodejs crawler
C language learning notes
[exercise -10] unread messages
Opencv learning log 29 -- gamma correction
Borg maze (bfs+ minimum spanning tree) (problem solving report)
Vs2019 initial use
Pytorch extract skeleton (differentiable)
(POJ - 3258) River hopper (two points)