2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 13
|
2016-08-23 21:00:35 +08:00
|
|
|
title:
|
2016-07-31 09:53:51 +08:00
|
|
|
zh-CN: 自定义校验规则
|
|
|
|
en-US: Customized validation
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-01-22 10:25:58 +08:00
|
|
|
|
2016-07-31 09:53:51 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-01-22 10:25:58 +08:00
|
|
|
密码校验实例。
|
|
|
|
|
2016-02-15 10:03:48 +08:00
|
|
|
这里使用了 `this.props.form.validateFields` 方法,在对第一次输入的密码进行校验时会触发二次密码的校验。
|
2016-01-22 10:25:58 +08:00
|
|
|
|
2016-07-31 09:53:51 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
Customized validation for Password.
|
|
|
|
|
2016-09-01 18:12:12 +08:00
|
|
|
To use `this.props.form.validateFields` method, when validating first password you enter will trigger the second password validation.
|
2016-07-31 09:53:51 +08:00
|
|
|
|
2016-01-22 10:25:58 +08:00
|
|
|
````jsx
|
2016-04-12 15:18:57 +08:00
|
|
|
import { Button, Form, Input, Row, Col } from 'antd';
|
2016-01-22 10:25:58 +08:00
|
|
|
import classNames from 'classnames';
|
2016-01-22 14:31:08 +08:00
|
|
|
const createForm = Form.create;
|
2016-01-22 10:25:58 +08:00
|
|
|
const FormItem = Form.Item;
|
|
|
|
|
|
|
|
function noop() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let Demo = React.createClass({
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
2016-07-25 16:08:18 +08:00
|
|
|
dirty: false,
|
2016-07-31 09:53:51 +08:00
|
|
|
passBarShow: false, // Whether to display a tooltip of password strength
|
2016-01-22 10:25:58 +08:00
|
|
|
rePassBarShow: false,
|
2016-07-31 09:53:51 +08:00
|
|
|
passStrength: 'L', // Password strength
|
2016-02-01 10:23:06 +08:00
|
|
|
rePassStrength: 'L',
|
2016-01-22 10:25:58 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-02-01 10:23:06 +08:00
|
|
|
handleSubmit() {
|
2016-01-23 16:18:22 +08:00
|
|
|
this.props.form.validateFields((errors, values) => {
|
2016-08-23 21:00:35 +08:00
|
|
|
if (errors) {
|
2016-01-22 10:25:58 +08:00
|
|
|
console.log('Errors in form!!!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('Submit!!!');
|
|
|
|
console.log(values);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getPassStrenth(value, type) {
|
|
|
|
if (value) {
|
|
|
|
let strength;
|
2016-07-31 09:53:51 +08:00
|
|
|
// Customized the password strength, here is just a simple example
|
2016-01-22 10:25:58 +08:00
|
|
|
if (value.length < 6) {
|
|
|
|
strength = 'L';
|
|
|
|
} else if (value.length <= 9) {
|
|
|
|
strength = 'M';
|
|
|
|
} else {
|
|
|
|
strength = 'H';
|
|
|
|
}
|
2016-08-23 21:00:35 +08:00
|
|
|
this.setState({
|
|
|
|
[`${type}BarShow`]: true,
|
|
|
|
[`${type}Strength`]: strength,
|
|
|
|
});
|
2016-01-22 10:25:58 +08:00
|
|
|
} else {
|
2016-08-23 21:00:35 +08:00
|
|
|
this.setState({
|
|
|
|
[`${type}BarShow`]: false,
|
|
|
|
});
|
2016-01-22 10:25:58 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
checkPass(rule, value, callback) {
|
|
|
|
const form = this.props.form;
|
|
|
|
this.getPassStrenth(value, 'pass');
|
|
|
|
|
2016-07-25 16:08:18 +08:00
|
|
|
if (form.getFieldValue('pass') && this.state.dirty) {
|
2016-01-25 15:00:03 +08:00
|
|
|
form.validateFields(['rePass'], { force: true });
|
2016-01-22 10:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
|
|
|
|
checkPass2(rule, value, callback) {
|
|
|
|
const form = this.props.form;
|
|
|
|
this.getPassStrenth(value, 'rePass');
|
|
|
|
|
|
|
|
if (value && value !== form.getFieldValue('pass')) {
|
2016-07-31 09:53:51 +08:00
|
|
|
callback('Two passwords you enter is inconsistent!');
|
2016-01-22 10:25:58 +08:00
|
|
|
} 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',
|
2016-05-11 09:32:33 +08:00
|
|
|
'ant-pwd-strength-high': strength === 'H',
|
2016-01-22 10:25:58 +08:00
|
|
|
});
|
|
|
|
const level = {
|
2016-07-31 09:53:51 +08:00
|
|
|
L: 'Low',
|
|
|
|
M: 'Middle',
|
|
|
|
H: 'High',
|
2016-01-22 10:25:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<ul className={classSet}>
|
2016-08-23 21:00:35 +08:00
|
|
|
<li className="ant-pwd-strength-item ant-pwd-strength-item-1" />
|
|
|
|
<li className="ant-pwd-strength-item ant-pwd-strength-item-2" />
|
|
|
|
<li className="ant-pwd-strength-item ant-pwd-strength-item-3" />
|
2016-01-22 10:25:58 +08:00
|
|
|
<span className="ant-form-text">
|
|
|
|
{level[strength]}
|
|
|
|
</span>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
2016-09-01 11:48:38 +08:00
|
|
|
const { getFieldDecorator } = this.props.form;
|
2016-01-22 10:25:58 +08:00
|
|
|
return (
|
2016-02-01 10:23:06 +08:00
|
|
|
<div>
|
2016-09-09 13:55:46 +08:00
|
|
|
<Form vertical style={{ maxWidth: 600 }}>
|
2016-08-04 17:45:15 +08:00
|
|
|
<Row type="flex" align="middle">
|
|
|
|
<Col span={12}>
|
|
|
|
<FormItem label="Password">
|
2016-09-01 11:48:38 +08:00
|
|
|
{getFieldDecorator('pass', {
|
|
|
|
rules: [
|
|
|
|
{ required: true, whitespace: true, message: 'Please enter your password' },
|
|
|
|
{ validator: this.checkPass },
|
|
|
|
],
|
|
|
|
})(
|
|
|
|
<Input type="password"
|
|
|
|
onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop}
|
|
|
|
autoComplete="off" id="pass"
|
|
|
|
onChange={(e) => {
|
|
|
|
console.log('Your password is stolen in this way', e.target.value);
|
|
|
|
}}
|
|
|
|
onBlur={(e) => {
|
|
|
|
const value = e.target.value;
|
|
|
|
this.setState({ dirty: this.state.dirty || !!value });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2016-04-12 15:18:57 +08:00
|
|
|
</FormItem>
|
|
|
|
</Col>
|
2016-08-04 17:45:15 +08:00
|
|
|
<Col span={12}>
|
2016-04-12 15:18:57 +08:00
|
|
|
{this.state.passBarShow ? this.renderPassStrengthBar('pass') : null}
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2016-08-04 17:45:15 +08:00
|
|
|
<Row type="flex" align="middle">
|
|
|
|
<Col span={12}>
|
|
|
|
<FormItem label="Confirm">
|
2016-09-20 13:49:38 +08:00
|
|
|
{getFieldDecorator('rePass', {
|
|
|
|
rules: [{
|
|
|
|
required: true,
|
|
|
|
whitespace: true,
|
|
|
|
message: 'Please confirm your password',
|
|
|
|
}, {
|
|
|
|
validator: this.checkPass2,
|
|
|
|
}],
|
|
|
|
})(
|
|
|
|
<Input type="password"
|
|
|
|
onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop}
|
|
|
|
autoComplete="off" id="rePass"
|
|
|
|
/>
|
|
|
|
)}
|
2016-04-12 15:18:57 +08:00
|
|
|
</FormItem>
|
|
|
|
</Col>
|
2016-08-04 17:45:15 +08:00
|
|
|
<Col span={12}>
|
2016-04-12 15:18:57 +08:00
|
|
|
{this.state.rePassBarShow ? this.renderPassStrengthBar('rePass') : null}
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2016-08-04 17:45:15 +08:00
|
|
|
<FormItem><Button type="primary" onClick={this.handleSubmit}>提交</Button></FormItem>
|
2016-04-12 15:18:57 +08:00
|
|
|
</Form>
|
2016-02-01 10:23:06 +08:00
|
|
|
</div>
|
2016-01-22 10:25:58 +08:00
|
|
|
);
|
2016-05-11 09:32:33 +08:00
|
|
|
},
|
2016-01-22 10:25:58 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
Demo = createForm()(Demo);
|
|
|
|
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
|
|
````
|
|
|
|
|
|
|
|
````css
|
|
|
|
.ant-pwd-strength {
|
|
|
|
display: inline-block;
|
|
|
|
margin-left: 8px;
|
|
|
|
line-height: 32px;
|
|
|
|
height: 32px;
|
|
|
|
vertical-align: middle;
|
|
|
|
}
|
|
|
|
|
|
|
|
.ant-pwd-strength-item {
|
|
|
|
float: left;
|
|
|
|
margin-right: 1px;
|
|
|
|
margin-top: 12px;
|
|
|
|
width: 19px;
|
|
|
|
height: 8px;
|
|
|
|
line-height: 8px;
|
|
|
|
list-style: none;
|
|
|
|
background-color: #f3f3f3;
|
|
|
|
transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
.ant-pwd-strength-item-1 {
|
|
|
|
border-top-left-radius: 6px;
|
|
|
|
border-bottom-left-radius: 6px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.ant-pwd-strength-item-2 {
|
|
|
|
width: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.ant-pwd-strength-item-3 {
|
|
|
|
border-top-right-radius: 6px;
|
|
|
|
border-bottom-right-radius: 6px;
|
|
|
|
margin-right: 8px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.ant-pwd-strength-low .ant-pwd-strength-item-1, .ant-pwd-strength-medium .ant-pwd-strength-item-1, .ant-pwd-strength-high .ant-pwd-strength-item-1 {
|
|
|
|
background-color: #FAC450;
|
|
|
|
}
|
|
|
|
|
|
|
|
.ant-pwd-strength-medium .ant-pwd-strength-item-2, .ant-pwd-strength-high .ant-pwd-strength-item-2 {
|
|
|
|
background-color: rgba(135, 208, 104, .6);
|
|
|
|
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#9987D068,endColorstr=#9987D068);
|
|
|
|
}
|
|
|
|
|
|
|
|
.ant-pwd-strength-high .ant-pwd-strength-item-3 {
|
|
|
|
background-color: #87D068;
|
|
|
|
}
|
|
|
|
````
|