有两种解决方案,下面上代码
<Form.Item>
{
getFieldDecorator("content",{
rules:[
{
required:true,
message:"内容不能为空"
},
{
max: 4,
message: '内容不能大于4个字'
},
// 方式一:正则匹配(提示错误,阻止表单提交)
{
pattern: /^[^\s]*$/,
message: '禁止输入空格',
},
],
//方式二:粗暴点<Input>不允许输入空格(其实是将e.tartget.value转成控件自己的值)
// 这个方法的用途非常强大,还可以结合upload做一些文件上传之后的回调处理
getValueFromEvent: (event) => {
return event.target.value.replace(/\s+/g,"")
},
})(<Input className={styles.inputStyle} placeholder={"请输入内容"}/>)
}
</Form.Item>