身份证号正则验证及提取性别出生年月出生时间

正则验证:

reg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;

通过身份证号获取性别:

getSex(idCard) {
      var sexStr = '';
      if (parseInt(idCard.slice(-2, -1)) % 2 == 1) {
        sexStr = '男';
      } else {
        sexStr = '女';
      }
      return sexStr;
    },

通过身份证号获取出生年月:

getBirth(idCard) {
      var birthday = '';
      if (idCard != null && idCard != '') {
        if (idCard.length == 15) {
          birthday = '19' + idCard.slice(6, 12);
        } else if (idCard.length == 18) {
          birthday = idCard.slice(6, 14);
        }
        birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-');
        //通过正则表达式来指定输出格式为:1990-01-01
      }
      return birthday;
    },

身份证输入立即美化

// 输入身份证搜索
    searchChange(){
      this.searchValue = this.searchValue.replace(/[^\d^X^x^\s]/g,'');

      if(this.searchValue.replace(/\s/g, '').length >= 7 && this.searchValue.replace(/\s/g, '').length<15){
        this.searchValue = this.searchValue.replace(/\s/g, '').slice(0, 6) + ' ' + this.searchValue.replace(/\s/g, '').slice(6);
      }else if(this.searchValue.replace(/\s/g, '').length >= 15){
        this.searchValue =this.searchValue.replace(/\s/g, '').slice(0, 6) + ' ' + this.searchValue.replace(/\s/g, '').slice(6,14)+  ' ' + this.searchValue.replace(/\s/g, '').slice(14);
      }
      this.searchValue =this.searchValue.trim();
     
    },

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值