The most useful JS method for verifying [ID card validity period] in history

Recently, there is a need to verify the start and end dates of the validity period of the input ID card.

Since the date selection box is not used, it is directly entered in text,

Therefore, some verification of the entered content is required.

Don't talk nonsense, go directly to the code

      let idCardPeriodDate = this.checkIdCardPeriodDate(this.state.idCardPeriodDate);
      if(!idCardPeriodDate){
          return;
      }
      // 校验通过
      this.setState({
          idCardPeriodDate: idCardPeriodDate.replace("至", " 至 "),
      });

The verification method is here:

  checkIdCardPeriodDate(value){
      console.log(value);
      if (checkNull(value, '身份证有效期')) {
        return false;
      }
      let idCardPeriodDate = value.replaceAll(" ", "")
                    .replaceAll(" ", "")
                    .replaceAll("•", "")
                    .replaceAll(".", "")
                    .replaceAll("-", "")
                    .replaceAll("_", "")
                    .replaceAll("/", "")
                    .replaceAll("永久", "长期");
      // 替换后再校验一遍
      if (checkNull(idCardPeriodDate, '身份证有效期')) {
        return false;
      }
      if(idCardPeriodDate.indexOf("至") <0 ){// 没有至
        message.destroy();
        message.error("【身份证有效期】格式必须为【YYYYMMDD 至 YYYYMMDD】");
        return false;
      }
      if(clearAllNoChineseChar(idCardPeriodDate).replaceAll("长期","").length > 1){
        // 有两个以及以上汉字
        message.destroy();
        message.error("【身份证有效期】格式必须为【YYYYMMDD 至 YYYYMMDD】");
        return false;
      }
      if(idCardPeriodDate.indexOf("至") === 0 || idCardPeriodDate.indexOf("至") === (idCardPeriodDate.length -1)){
        // 至在第一或者最后一位
        message.destroy();
        message.error("【身份证有效期】格式必须为【YYYYMMDD 至 YYYYMMDD】");
        return false;
      }
      let startIdDate = idCardPeriodDate.split("至")[0];
      let endIdDate = idCardPeriodDate.split("至")[1];
      console.log("startIdDate", startIdDate);
      console.log("endIdDate", endIdDate);

      console.log("parse", Date.parse(
          startIdDate.substring(0,4)+"-"+startIdDate.substring(4,6)+"-"+startIdDate.substring(6,8)
        ));

      // 校验前半部分
      if(startIdDate.length !==8){
        message.destroy();
        message.error("【身份证有效期】开始日期必须为【YYYYMMDD】格式");
        return false;
      }
      if(isNaN(Date.parse(
          startIdDate.substring(0,4)+"-"+startIdDate.substring(4,6)+"-"+startIdDate.substring(6,8)
      ))){
        message.destroy();
        message.error("【身份证有效期】开始日期有误");
        return false;
      }

      // 校验后半部分
      if(endIdDate !== '长期' && endIdDate.length !==8){
        message.destroy();
        message.error("【身份证有效期】结束日期必须为【YYYYMMDD】格式");
        return false;
      }

      if(endIdDate !== '长期' && isNaN(Date.parse(
        endIdDate.substring(0,4)+"-"+endIdDate.substring(4,6)+"-"+endIdDate.substring(6,8)
      ))){
        message.destroy();
        message.error("【身份证有效期】结束日期有误");
        return false;
      }

      if(endIdDate !== '长期' && startIdDate >= endIdDate){
        message.destroy();
        message.error("【身份证有效期】开始日期必须大于开始日期");
        return false;
      }

      let nowDate = getDate();
      if(startIdDate > nowDate){
        message.destroy();
        message.error("【身份证有效期】开始日期不可大于【"+nowDate+"】");
        return false;
      }

      if(endIdDate !== '长期' && nowDate >= endIdDate){
        message.destroy();
        message.error("【身份证有效期】结束日期必须大于【"+nowDate+"】");
        return false;
      }

      return idCardPeriodDate;
  }
export const getDate = function() {
  const date = new Date(); // 获取时间
  const year = date.getFullYear(); // 获取年
  const month = date.getMonth() + 1; // 获取月
  const strDate = date.getDate(); // 获取日
  return year + getNum(month) + getNum(strDate);
};
export const getNum = function(i) {
  return i < 10 ? '0' + i : i
};

Finish

Guess you like

Origin blog.csdn.net/u013282737/article/details/129202822