--- order: 13 title: 自定义校验规则 --- 密码校验实例。 这里使用了 `this.props.form.validateFields` 方法,在对第一次输入的密码进行校验时会触发二次密码的校验。 ````jsx import { Button, Form, Input, Row, Col } from 'antd'; import classNames from 'classnames'; const createForm = Form.create; const FormItem = Form.Item; function noop() { return false; } let Demo = React.createClass({ getInitialState() { return { passBarShow: false, // 是否显示密码强度提示条 rePassBarShow: false, passStrength: 'L', // 密码强度 rePassStrength: 'L', }; }, handleSubmit() { this.props.form.validateFields((errors, values) => { if (!!errors) { console.log('Errors in form!!!'); return; } console.log('Submit!!!'); console.log(values); }); }, getPassStrenth(value, type) { if (value) { let strength; // 密码强度的校验规则自定义,这里只是做个简单的示例 if (value.length < 6) { strength = 'L'; } else if (value.length <= 9) { strength = 'M'; } else { strength = 'H'; } if (type === 'pass') { this.setState({ passBarShow: true, passStrength: strength }); } else { this.setState({ rePassBarShow: true, rePassStrength: strength }); } } else { if (type === 'pass') { this.setState({ passBarShow: false }); } else { this.setState({ rePassBarShow: false }); } } }, checkPass(rule, value, callback) { const form = this.props.form; this.getPassStrenth(value, 'pass'); if (form.getFieldValue('pass')) { form.validateFields(['rePass'], { force: true }); } callback(); }, checkPass2(rule, value, callback) { const form = this.props.form; this.getPassStrenth(value, 'rePass'); if (value && value !== form.getFieldValue('pass')) { callback('两次输入密码不一致!'); } else { callback(); } }, renderPassStrengthBar(type) { const strength = type === 'pass' ? this.state.passStrength : this.state.rePassStrength; const classSet = classNames({ 'ant-pwd-strength': true, 'ant-pwd-strength-low': strength === 'L', 'ant-pwd-strength-medium': strength === 'M', 'ant-pwd-strength-high': strength === 'H' }); const level = { L: '低', M: '中', H: '高' }; return (