antd from验证input框只能输入数字

版权声明:如需转载,请注明出处 https://blog.csdn.net/zr15829039341/article/details/82745239

antd from验证input框只能输入数字

问题:Input框中如果只能需要输入Id,也就是数字型字符串,需要进行验证。

解决办法

对其进行实时正则验证,

/^[1-9]\d*$/

代码实现:

<Form.Item label='ID' >
     {
        getFieldDecorator('id', {
             rules:[{
                 required:false,
                 pattern: new RegExp(/^[1-9]\d*$/, "g"),
                 message: '请输入正确的ID'
             }],
             getValueFromEvent: (event) => {
                 return event.target.value.replace(/\D/g,'')
             },
             initialValue:''
        })(<Input />)
     }
 </Form.Item>

这样的操作就是输入框只能输入数字,输入字符是不被允许的,也就是输入不进去字符,从前端确保提交数据格式的正确性

猜你喜欢

转载自blog.csdn.net/zr15829039341/article/details/82745239