| JS-如何控制input只能输入正整数
					当前位置:点晴教程→知识管理交流
					
					→『 技术文档交流 』
					
				 
  :JS-如何控制input只能输入正整数  示例代码如下 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Input框只允许输入数字</title> </head> <body> <input type="text" id="inputBox" oninput="filterInput(event)"> <div id="warningMessage" style="color: red;"></div> <script> function filterInput(event) { const inputBox = event.target; const inputValue = inputBox.value; 
 // 过滤特殊符号和空格 const filteredValue = inputValue.replace(/[^\d]/g, ''); 
 if (inputValue !== filteredValue) { document.getElementById('warningMessage').innerText = '只能输入数字,请勿输入特殊字符或空格'; } else { document.getElementById('warningMessage').innerText = ''; } 
 inputBox.value = filteredValue; } </script> </body> </html> 该文章在 2024/7/19 15:43:00 编辑过 | 关键字查询 相关文章 正在查询... |