Ant Design4.x rule验证规则的两种写法(函数与正则表达式)

2023-02-27 14:06:46 浏览数 (1)

1 函数验证

代码语言:javascript复制
 //函数
 const checkPrice = (rule, value) => {
    if (value.number > 0) {
      return Promise.resolve();
    }

    return Promise.reject('Price must be greater than zero!');
  };

//表单
 <Form.Item
        name="price"
        label="Price"
        rules={[
          {
            validator: checkPrice,
          },
        ]}
  >
      <PriceInput />
  </Form.Item>

2 正则表达式

代码语言:javascript复制
rules={
    [
        {
            required: true,
            message: '请输入个数'
        },
        {
            pattern:/^(d|[1-9]d|100)$/,
            message:'请输入0~100之间的数字'
        }
    ]
}

0 人点赞