Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to validate password using react with ant design

Tags:

reactjs

antd

I want to validate password using ant design with react. But, ant design doesn't have any validation.How can I do that ? Please help me.

 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>
  );
});
like image 345
Khin san Avatar asked Jul 06 '18 04:07

Khin san


People also ask

Is ant design good for react?

What do you like best about Ant Design React? Its very easy to use and well documented which help to easily implement and use. ANT provide almost all type of components and design which is required to create an website.


1 Answers

Use validator rule:

  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>
like image 77
froston Avatar answered Oct 01 '22 00:10

froston