表单验证联动

import {Picker, Card, WingBlank, WhiteSpace ,List,InputItem} from 'antd-mobile';
import { Component } from 'react';
import { connect } from 'dva';
import styles from './IndexPage.css';

const colors = [
  {
    label:'其他',
    value: '01',
    reg: /^[0-9]+$/
  },
  {
    label:'二代身份证',
    value: '02',
    reg: /^(\d{6})(19)(\d{2})(0|1)(\d)([0-3])(\d)(\d{3})([0-9]|X|x)$/
  },
  {
    label:'护照',
    value: '03',
    reg: /^[0-9]+$/
  },
];

class Self  extends Component{
  state = {
    error: '',
    data: [],
    val: '',
    cols: 1,
    validate: false,
    colorValue: ['01'],
    numReg: /^(\d{6})(19)(\d{2})(0|1)(\d)([0-3])(\d)(\d{3})([0-9]|X|x)$/
  };
  
  onChangeColor = (color) => {
    console.log(color)
    this.setState({colorValue: color});
    this.validateInfo(color, this.state.val)
  };
  handleInputVal = val => {
    this.setState({val: val});
    this.validateInfo(this.state.colorValue, val)
  }

  validateInfo = (type, val) =>{
    const that = this
    const colorObj = colors.filter(item => that.state.colorValue.includes(item.value))
    const validate = colorObj.length ? colorObj[0].reg.test(val) : false 
    this.setState({ error: validate ? '' : styles.error, validate: validate })
  }

    render(){
            return(
                <div>
     <WingBlank size="lg">
        <WhiteSpace size="lg" />
        <Card>
          <Card.Header
            title="This is title"
            
          />
          <Card.Body>
          <Picker
          data={colors}
          value={this.state.colorValue}
          cols={1}
          onChange={this.onChangeColor}
        >
          <List.Item arrow="horizontal">证件类型</List.Item>
        </Picker>


        <InputItem
            onChange={this.handleInputVal}
            placeholder="请输入证件号码"
            moneyKeyboardAlign="left"
            error={this.state.error}
          >证件号码</InputItem>
          </Card.Body>
          <Card.Footer content="footer content" extra={<div>extra footer content</div>} />
        </Card>
        <WhiteSpace size="lg" />
      </WingBlank>
                </div>
            )
       
    }
}

export default connect()(Self)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值