You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

react-number-format输入时提前显示格式符号问题求助

解决react-number-format手机号输入提前显示格式符号的问题

使用PatternFormat组件实现手机号格式化时,设置格式为(###) ###-####会出现输入单个字符就提前显示括号、短横线等格式符号的问题,需要让格式符号仅在输入到对应位置时才显示。

只需给组件添加allowEmptyFormatting={false}属性即可解决:

import { PatternFormat } from 'react-number-format';

<PatternFormat 
   value={value} 
   className='form-control'   
   format="(###) ###-####"
   allowEmptyFormatting={false}
/>

allowEmptyFormatting默认值为true,会在输入内容未填满对应位置时就渲染所有格式符号;设置为false后,格式符号会跟随输入内容逐步显示:输入前3位数字后才会显示闭合括号,输入第4-6位数字后才会显示空格和短横线,完全匹配期望的输入效果。

内容的提问来源于stack exchange,提问作者Brijal Kansara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 03:03:17