react+antd系列之Form表单(2):格式限制验证

格式限制

antd中表单的功能很多,下面就为大家整理了一下antd中常用的几种表单输入格式验证:

1. 输入框不能为空限制,如下:

  {getFieldDecorator('name', {
            rules: [{
            required: true,
            message: '名称不能为空',
          }],
         })(
      <Input placeholder="请输入名称" />
  )}

2. 输入框字符限制,如下:

字符长度范围限制:

   {getFieldDecorator('password', {
            rules: [{
              required: true,
              message: '密码不能为空',
            }, {
            min:4,
            message: '密码不能少于4个字符',
          }, {
            max:6,
            message: '密码不能大于6个字符',
          }],
         })(
        <Input placeholder="请输入密码" type="password"/>
   )}

字符长度限制:

  {getFieldDecorator('nickname', {
            rules: [{
            required: true,
            message: '昵称不能为空',
          }, {
            len: 4,
            message: '长度需4个字符',
          }],
         })(
       <Input placeholder="请输入昵称" />
  )}

3. 自定义校验

   {getFieldDecorator('passwordcomfire', {
            rules: [{
              required: true,
              message: '请再次输入密码',
            }, {
              validator: passwordValidator
          }],
         })(
            <Input placeholder="请输入密码" type="password"/>
   )}
   
     //  密码验证
  const passwordValidator = (rule, value, callback) => {
    const { getFieldValue } = form;
    if (value && value !== getFieldValue('password')) {
        callback('两次输入不一致!')
    }

    // 必须总是返回一个 callback,否则 validateFields 无法响应
    callback();
  }

validator属性自定义效验,必须返回一个callback

4.whitespace空格报错

  {getFieldDecorator('hobody', {
            rules: [{
              whitespace: true,
              message: '不能输入空格',
          } ],
         })(
            <Input placeholder="请输入昵称" />
  )}

若输入只有一个空格,则会报错

5.pattern正则验证

 {getFieldDecorator('qbc', {
            rules: [{
              message:'只能输入数字',
              pattern: /^[0-9]+$/
          } ],
         })(
            <Input placeholder="请输入ABC" />
)}

如果输入的不是数字,则提示错误

完整代码地址:
https://gitee.com/hope93/antd-form2

  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
React中,清空可以通过AntdForm组件提供的`resetFields`方法来实现。具体步骤如下: 1. 在的父组件中引入`Form`组件,并将的所有控件都用`Form.Item`包裹起来,每个`Form.Item`需要设置`name`属性,这个属性值需要与`getFieldDecorator`方法中的`id`参数保持一致。 ```jsx import { Form, Input, Button } from 'antd'; class MyForm extends React.Component { render() { const { getFieldDecorator } = this.props.form; return ( <Form> <Form.Item label="用户名" name="username"> {getFieldDecorator('username')(<Input />)} </Form.Item> <Form.Item label="密码" name="password"> {getFieldDecorator('password')(<Input.Password />)} </Form.Item> <Form.Item> <Button type="primary" onClick={this.handleSubmit}>提交</Button> <Button onClick={this.handleReset}>重置</Button> </Form.Item> </Form> ); } } ``` 2. 在的父组件中定义`handleSubmit`和`handleReset`方法。`handleSubmit`方法用于提交,`handleReset`方法用于清空。 ```jsx class MyForm extends React.Component { handleSubmit = e => { e.preventDefault(); this.props.form.validateFields((err, values) => { if (!err) { console.log('Received values of form: ', values); } }); }; handleReset = () => { this.props.form.resetFields(); }; render() { //... } } ``` 3. 在的父组件中将`MyForm`组件包裹在`Form.create`函数中,生成一个新的高阶组件,并将其导出。 ```jsx const WrappedMyForm = Form.create({ name: 'my_form' })(MyForm); export default WrappedMyForm; ``` 这样,当用户点击中的“重置”按钮时,中的所有控件都会被清空。如果想要清空中的某一个控件,可以通过`setFieldsValue`方法来清空,具体可见前面的回答。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值