【问题标题】:I want to validate password using react with ant design我想使用与 ant design 的反应来验证密码
【发布时间】:2019-11-27 18:11:25
【问题描述】:

我想使用带有 react 的 ant design 来验证密码。但是,蚂蚁设计没有任何验证。我该怎么做?请帮帮我。

 const LoginForm = Form.create()(props => {
  const { getFieldDecorator } = props.form;
  return (
    <Form onSubmit={props.onSubmit} className="form-size form-margin">
      <FormItem>
        {getFieldDecorator("email", {
          rules: [
            {
              type: "email",
              message: "The input is not valid E-mail!"
            },
            { required: true, message: "Please input your username!" }
          ]
        })(<Input placeholder="Email" />)}
      </FormItem>
      <FormItem>
        {getFieldDecorator("password", {
          rules: [{ required: true, message: "Please input your Password!" }]
        })(<Input type="password" placeholder="Password" />)}
      </FormItem>
      <FormItem>
        <Button type="primary" htmlType="submit" className="login-form-button">
          Log in
        </Button>
      </FormItem>
      <span style={{ color: "red" }}>
        {props.loginStatus ? "" : props.loginMessage}
      </span>
    </Form>
  );
});

【问题讨论】:

  • Form 提供了一个onFieldsChange 属性,您可以在其中提供回调函数,也许您想在那里进行自己的验证?

标签: reactjs antd


【解决方案1】:

使用validator 规则:

  const validatePassword = (rule, value, callback) => {
    if (value && value !== "Secret") {
      callback("Error!");
    } else {
      callback();
    }
  };

  <FormItem>
    {getFieldDecorator("password", {
      rules: [
        { required: true, message: "Please input your Password!" },
        { validator: validatePassword }
      ]
    })(
      <Input 
        type="password" 
        placeholder="Password" 
      />
    )}
  </FormItem>

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 2021-01-11
    • 2019-11-22
    • 2017-12-19
    • 2017-06-23
    • 1970-01-01
    • 2016-09-27
    • 2018-12-19
    • 2019-11-26
    相关资源
    最近更新 更多