身份證號正則驗證及提取性別出生年月出生時間

正則驗證:

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();
     
    },

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章