代码:
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> ) } } `