我正在尝试创建密码确认功能,只有在用户离开确认字段后才会显示错误.我正在与Facebook的React JS合作.这是我的输入组件:
<input type="password" placeholder="Password (confirm)" valueLink={this.linkState('password2')} onBlur={this.renderPasswordConfirmError()} />
这是renderPasswordConfirmError:
renderPasswordConfirmError: function() { if (this.state.password !== this.state.password2) { return ( <div> <label className="error">Please enter the same password again.</label> </div> ); } return null; },
当我运行页面时,输入冲突的密码时不会显示该消息.
解决方法
这里有一些问题.
1:onBlur期望回调,并且您调用renderPasswordConfirmError并使用返回值,该值为null.
2:你需要一个地方来呈现错误.
3:你需要一个标志来跟踪“我验证”,你会在模糊时设置为true.如果需要,您可以将焦点设置为false,具体取决于您所期望的行为.
handleBlur: function () { this.setState({validating: true}); },render: function () { return <div> ... <input type="password" placeholder="Password (confirm)" valueLink={this.linkState('password2')} onBlur={this.handleBlur} /> ... {this.renderPasswordConfirmError()} </div> },renderPasswordConfirmError: function() { if (this.state.validating && this.state.password !== this.state.password2) { return ( <div> <label className="error">Please enter the same password again.</label> </div> ); } return null; },
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。