【问题标题】:Email validation in React ant design formReact ant 设计表单中的电子邮件验证
【发布时间】:2022-01-26 16:20:20
【问题描述】:

我正在使用 React.js 中的 ant 设计。我正在尝试使用 ant 设计规则验证电子邮件。我的正则表达式不起作用。

<Form.Item>
    {getFieldDecorator('email', {
        initialValue:null,
        rules: [
            {
                required: false,
                pattern: new RegExp("/\S+@\S+\.\S+/"),
                message:
                    'Enter a valid email address!',
            },
        ],
    })(
        <Input
            className="form-control"
            type="text"
            placeholder="Email"
        />,
    )}
</Form.Item>

【问题讨论】:

标签: reactjs antd


【解决方案1】:

在规则中,您可以直接使用值为email type 键,而不是正则表达式模式。

代码示例https://codesandbox.io/s/stackoverflowantdemail-9jckh?file=/index.js:916-1352

<Form.Item>
  {getFieldDecorator("email", {
    rules: [
      {
        required: true,
        type: "email",
        message: "The input is not valid E-mail!",
      },
    ],
  })(<Input placeholder="Email" />)}
</Form.Item>;

【讨论】:

    【解决方案2】:
    <Form.Item
        name="email"
        label="E-mail"
        rules={[
          {
            type: 'email',
            message: 'The input is not valid E-mail!',
          },
          {
            required: true,
            message: 'Please input your E-mail!',
          },
        ]}
      >
        <Input />
      </Form.Item>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2011-05-27
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多