javascript - 如何在 React Js 中设置匹配密码

标签 javascript reactjs react-redux reactive-programming

我需要一些帮助如何在 React js 中匹配密码。我使用了 ant design,第一个密码有效,但为了符合密码,我声明它不起作用,我该怎么做

handlePasswordChange = event => {
  this.setState({
    password: event.target.value,
  });
};
handleConfirmPassword = event => {
  if (event.handleConfirmPassword !== event.handlePasswordChange) {
    message.error('error');
  }
};

这些很有趣,下面是 Ant 设计

<FormItem {...styles.formItemLayout} label="Password">
  {getFieldDecorator('Password', {
    rules: [{ required: true, message: 'Password is Required!' }],
  })(
    <Input
      onChange={this.handlePasswordChange}
      name="password"
      type="password"
      value={password}
      style={styles.margin}
    />,
  )}
</FormItem>
<FormItem {...styles.formItemLayout} label="Confirm Password">
  {getFieldDecorator('Confirm Password', {
    rules: [{ required: true, message: 'Confirm your Password!' }],
  })(
    <Input
      name="password"
      type="password"
      style={styles.margin}
      onChange={this.handleConfirmPassword}
    />,
  )}
</FormItem>

最佳答案

假设您的密码和确认密码都处于状态。

this.state = {
    password: '',
    confirmPassword: ''
}

handleSubmit = () => {
    const { password, confirmPassword } = this.state;
    // perform all neccassary validations
    if (password !== confirmPassword) {
        alert("Passwords don't match");
    } else {
        // make API call
    }
}

关于javascript - 如何在 React Js 中设置匹配密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51143800/

相关文章:

javascript - 允许 Select2 元素中的标签

javascript - window.location.href 但地址栏发生变化的地方

reactjs - react 路由器 dom TypeScript TS2322 : Type 'typeof Index' is not assignable to type

javascript - 在KeyPress上加载新的新组件-reactjs

javascript - 需要有关react-redux f7 v3 模板的意外字符错误的帮助

javascript - 分割 Javascript 文件

javascript - 将具有随机分辨率的图像居中到固定大小的父 div 的中心

reactjs - 我们可以将sectionList(或scrollView)的粘性标题粘贴到 ScrollView 顶部的相对位置吗?

reactjs - 如何将异步值的返回值分配给reducer的初始状态?

reactjs - React Native : when you navigate away from a component, 该组件曾经被卸载过吗?