js 验证身份证号,根据身份证获取出生年月/性别

// 校验身份证
export const checkNumber = (number) => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  return reg.test(number);
};

// 根据身份证,获取 出生年月日
export const getBirthBySfzh = (sfzh) => {
  const year = sfzh.substr(6, 4); // 身份证年
  const month = sfzh.substr(10, 2); // 身份证月
  const date = sfzh.substr(12, 2); // 身份证日
  const birth = `${year}-${month}-${date}`;
  return birth;
};
// 根据身份证,获取 性别
export const getSexBySfzh = (sfzh) => {
  const num = sfzh.substring(16, 17);
  return num % 2 === 0 ? '女' : '男';
};


// 校验手机电话格式
export const checkPhone = (phone) => {
  const myreg = /^(1\d{10})$/;
  return myreg.test(phone);
};

// 校验手机电话格式
export const checkMobilePhone = (phone) => {
  const myreg = /^(\d{7})$/;
  return myreg.test(phone);
};

export const isNumber = (number) => {
  const myreg = /^(\d+)$/;
  return myreg.test(number);
};

 

posted @ 2020-12-31 10:50  拈花醉  阅读(501)  评论(0编辑  收藏  举报