jsx
import React from 'react';
import { Input, Icon } from 'antd';
export default class InputMobile extends React.Component {
constructor(props){
super(props);
this.ajaxFlag = true;
this.state = {
value: this.props.default || '',
}
}
isPhone = (tel) => {
let reg = /^1[0-9]{10}$/;
return reg.test(tel);
};
checkPhone = (value) => {
if (value.substr(0, 1) === '1') {
return value.length <= 11;
} else {
return false;
}
};
changeValue = (e) => {
let value = e.target.value;
value = value.replace(/\D/g,'');
this.setState({ value });
if(this.checkPhone(value)){
this.props.callback(value);
}else{
this.props.callback(value, '请输入正确的手机号');
}
};
mobileOnBlur = (e) => {
let value = e.target.value;
if(value){
if(this.isPhone(value)){
this.props.callback(value);
}else{
this.props.callback(value, '请输入正确的手机号');
}
}else{
this.props.callback(value, '请输入手机号');
}
};
emitEmpty(){
this.setState({ value: '' });
this.props.callback();
};
render(){
const { value } = this.state;
return(
<Input
size="large"
maxLength={11}
autoComplete="off"
placeholder="手机号"
onChange={this.changeValue}
onBlur={this.mobileOnBlur}
value={value}
suffix={
<span>
{
value ?
<Icon
type="close-circle"
onClick={() => this.emitEmpty()}
/>
:
null
}
</span>
}
/>
)
}
}