antd Form中getFieldDecorator校验不能输入空字符和特殊字符
程序员文章站
2022-04-05 11:05:16
...
//校验大于0小于等于99的数
checkNum = (rule, value, callback) => {
let x = value -0
if(x >= 0 && x <= 99){
let a = new RegExp(/^\d+(\.\d{1,2})?$/)
if(!a.test(x)){
return callback("请保留两位小数");
}
return callback();
}else{
return callback("请输入小于等于99的数并保留两位小数");
}
}
<Form.Item label="单价:">
{getFieldDecorator('unitPrice', {
rules: [
{ validator: this.checkNum },
{required: true, message: '请设置单价'},
{ validator: (rule, value, callback) => {
if(value.trim()==""){
return callback("请输入非空字符")
}else{
return callback();
}
}},
{ validator: (rule, value, callback) => {
let reg =new RegExp("[`~!%@#$^&*()=|{}':;',\\[\\]<>《》/?~!@#¥……&*()|{}【】‘;:”“'\"。,、?]");
if(reg.test(value)){
return callback("不能输入该特殊字符")
}else{
return callback();
}
}},
],
initialValue: rowData?rowData.unitPrice:""
})(<Input
placeholder="请设置单价"
/>)}
</Form.Item>