当前位置:网站首页>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, ''))
边栏推荐
- F - birthday cake (Shandong race)
- 7-1 understand everything (20 points)
- (POJ - 3579) median (two points)
- Nodejs crawler
- 第 300 场周赛 - 力扣(LeetCode)
- New to redis
- Information security - security professional name | CVE | rce | POC | Vul | 0day
- What is the difficulty of programming?
- 1689. Ten - the minimum number of binary numbers
- 日期加1天
猜你喜欢
Codeforces Round #799 (Div. 4)A~H
C language is the watershed between low-level and high-level
Quick to typescript Guide
1013. Divide the array into three parts equal to and
The concept of C language array
Basic Q & A of introductory C language
Analysis of protobuf format of real-time barrage and historical barrage at station B
Penetration test (3) -- Metasploit framework (MSF)
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Maximum product (greedy)
随机推荐
(POJ - 3258) River hopper (two points)
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Nodejs+vue online fresh flower shop sales information system express+mysql
Is the sanic asynchronous framework really so strong? Find truth in practice
1855. Maximum distance of subscript alignment
Frida hook so layer, protobuf data analysis
C language learning notes
Codeforces Round #797 (Div. 3)无F
Openwrt source code generation image
Some problems encountered in installing pytorch in windows11 CONDA
1013. Divide the array into three parts equal to and
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
Suffix expression (greed + thinking)
1903. Maximum odd number in string
读取和保存zarr文件
Penetration test (7) -- vulnerability scanning tool Nessus
QT模拟鼠标事件,实现点击双击移动拖拽等
CEP used by Flink
[exercise 4-1] cake distribution