简体   繁体   English

React ant 设计表单中的电子邮件验证

[英]Email validation in React ant design form

I am working with ant design in React.js.我正在使用 React.js 中的 ant 设计。 I'm trying to validate email with ant design rules.我正在尝试使用 ant 设计规则验证电子邮件。 My regex does not work.我的正则表达式不起作用。

<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>

In rules you can directly make use of type key with value email instead regex pattern.在规则中,您可以直接使用带有值电子邮件的类型键而不是正则表达式模式。

Code sample https://codesandbox.io/s/stackoverflowantdemail-9jckh?file=/index.js:916-1352代码示例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>
<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM