当前位置:网站首页>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]/g
It'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, ''))
边栏推荐
- Borg maze (bfs+ minimum spanning tree) (problem solving report)
- QT实现圆角窗口
- Opencv learning log 29 -- gamma correction
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- 渗透测试 ( 1 ) --- 必备 工具、导航
- [exercise -10] unread messages
- Analysis of protobuf format of real-time barrage and historical barrage at station B
- Basic Q & A of introductory C language
- Auto. Getting started with JS
- “鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
猜你喜欢
Programmers, what are your skills in code writing?
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
window11 conda安装pytorch过程中遇到的一些问题
Borg maze (bfs+ minimum spanning tree) (problem solving report)
Codeforces Round #801 (Div. 2)A~C
(POJ - 3579) median (two points)
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
C language must memorize code Encyclopedia
7-1 understand everything (20 points)
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
随机推荐
Flask框架配置loguru日志库
日期加1天
F - birthday cake (Shandong race)
1855. Maximum distance of subscript alignment
Web based photo digital printing website
New to redis
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
(POJ - 3685) matrix (two sets and two parts)
7-1 understand everything (20 points)
Penetration test (1) -- necessary tools, navigation
Sword finger offer II 019 Delete at most one character to get a palindrome
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
AcWing:第56场周赛
B - Code Party (girls' competition)
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
2027. Minimum number of operations to convert strings
Gartner: five suggestions on best practices for zero trust network access
1689. Ten - the minimum number of binary numbers
Flask框架配置loguru日志庫
921. Minimum additions to make parentheses valid