加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React 实现 身份证15位转18位

发布时间:2020-12-15 07:15:10 所属栏目:百科 来源:网络整理
导读:代码: import React,{Component} from 'react'; import './list.less'; export default class IDcard extends Component { constructor(props) { super(props); this.state = { newID:'',oldID:'',} } onChange(key,e,value) { this.setState({oldID:e.targ

代码:

import React,{Component} from 'react';
    import './list.less';
    export default class IDcard extends  Component {
      constructor(props) {
       super(props);
       this.state = {
       newID:'',oldID:'',}
    }
      onChange(key,e,value) {
        this.setState({oldID:e.target.value});
      }
      
      onClick(){
        let v = new Array();
        let vs = '10X98765432';
        v.push(2,4,8,5,10,9,7,3,6,1,2,7)
        let oldID=this.state.oldID
        if(oldID.length !==15){
          alert("请输入15位你的身份证号码!");
          return;
        }
        let month = oldID.substring(8,10);
        if (!this.checkMonth(month)){
          alert("你输入的身份证格式不正确,月份填写错误!")
          return;
        }
    
        let year = "19" + oldID.substring(6,8);
        let day = oldID.substring(10,12);
        if(!this.checkDay(year,month,day)){
          alert("你输入的身份证的个格式不正确,日期格填写错误");
          return;
        }
    
        let cardID17 = oldID.substring(0,6)+"19"+oldID.substring(6);
        let N = 0,R = -1,T = '0',j = 0,cardID18='';
        
        for (var i = 16;i >=0;i--){
          N += parseInt(cardID17.substring(i,i+1))*v[j];
          j++;
        }
        R = N % 11;
        T = vs.charAt(R)
        cardID18 = cardID17 + T;
        this.setState({newID:cardID18})
    
      }
    
      checkMonth(month){
        if (month<1 || month >12){
          return false;
        }
        return true;
      }
      
      checkDay(year,day){
        let Mday = 0;
        if(day<1 || day>31){
          return false;
        }
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)  Mday =31;
        if(month==4||month==6||month==9||month==11) Mday = 30;
        if(month===2) Mday = this.isLeapYear(year);
        if(day>Mday) return false;
        return true
      }
    
      isLeapYear(year){
        return (year % 4 === 0 && year % 100 !==0) || (year % 400 === 0)
      }
    
      render(){
        return(
          <div>
            15位号码:<input className='normal'  name='oldID' value={this.state.oldID} onChange={this.onChange.bind(this,'oldID')}/>
                <br/>
                <br/>
            18位号码:<input className='normal'   value={this.state.newID}/>
                <br/>
                <br/>
            <input type='button'   value='转换' onClick={this.onClick.bind(this)}/>
          </div>
        )
      }
    }
 `

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读