javascript - react-hook-form 为什么数字输入类型忽略maxLength?
问题描述
我正在使用 react-hook-form 创建一个 4 位数字的输入,这不会让用户输入字母或字符,但它会忽略该maxLength
值。我能够获得仅允许数字的输入,但我无法限制长度。
这是我的输入:
<input
id="fourNumbers"
required
type="number"
name="fourNumbers"
ref={register({ required: true, pattern: /[0-9]{4}/ })}
placeholder="0000"
maxLength="4"
minLength="4"
/>
解决方案
取自https://stackoverflow.com/a/9555196/1772933
对 type="number" 的输入使用 max 属性。它将指定您可以插入的最大可能数字
<input type="number" max="9999" />
如果同时添加最大值和最小值,则可以指定允许值的范围:
<input type="number" min="1000" max="9999" />
推荐阅读
- java - Spring Cloud Stream Kafka 消费者坚持长时间运行的作业和 max.poll.interval.ms 的大值
- ns2 - 分析 NS2 中的能量
- sql - 从另一个表中的任何行中选择包含数据的行
- javascript - 如何在javascript中检查当前浏览器是支付宝手机浏览器?
- visual-studio-code - Visual Studio Code 未签名?无法在 macOS Catalina 上运行
- javascript - 生成 18 位唯一编号
- flutter - Flutter:使用参数从 App A 启动 App B
- javascript - 反应高阶函数返回钩子
- hibernate - 如何在 Hibernate 中编写 NVL 的条件查询
- python - pygame中绘制的图像与给出的位置不匹配