微信小程序身份证校验封装

2022-05-16  本文已影响0人  清风昙

微信小程序身份证校验封装
utils目录下新建idcard.js,内容如下:

/**
 * 去掉字符串头尾空格
 */
function trim(str) {
  return str.replace(/(^\s*)|(\s*$)/g, "");
}

/**
 * 身份证号验证
 */
function checkIdCard(idCard) {
  idCard = trim(idCard.replace(/ /g, "")); //去掉字符串头尾空格
  if (idCard.length == 15) {
    return isValidityBrithBy15IdCard(idCard); //进行15位身份证的验证
  } else if (idCard.length == 18) {
    var a_idCard = idCard.split(""); // 得到身份证数组
    if (isValidityBrithBy18IdCard(idCard) && isTrueValidateCodeBy18IdCard(a_idCard)) { //进行18位身份证的基本验证和第18位的验证
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

/**
 * 判断身份证号码为18位时最后的验证位是否正确
 * @param a_idCard 身份证号码数组
 * @return
 */
function isTrueValidateCodeBy18IdCard(a_idCard) {
  var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; // 加权因子
  var ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
  var sum = 0; // 声明加权求和变量
  if (a_idCard[17].toLowerCase() == 'x') {
    a_idCard[17] = 10; // 将最后位为x的验证码替换为10方便后续操作
  }
  for (var i = 0; i < 17; i++) {
    sum += Wi[i] * a_idCard[i]; // 加权求和
  }
  var valCodePosition = sum % 11; // 得到验证码所位置
  if (a_idCard[17] == ValideCode[valCodePosition]) {
    return true;
  } else {
    return false;
  }
}

/**
 * 验证18位数身份证号码中的生日是否是有效生日
 * @param idCard 18位书身份证字符串
 * @return
 */
function isValidityBrithBy18IdCard(idCard18) {
  var year = idCard18.substring(6, 10);
  var month = idCard18.substring(10, 12);
  var day = idCard18.substring(12, 14);
  var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
  // 这里用getFullYear()获取年份,避免千年虫问题
  if (temp_date.getFullYear() != parseFloat(year) ||
    temp_date.getMonth() != parseFloat(month) - 1 ||
    temp_date.getDate() != parseFloat(day)) {
    return false;
  } else {
    return true;
  }
}

/**
 * 验证15位数身份证号码中的生日是否是有效生日
 * @param idCard15 15位书身份证字符串
 * @return
 */
function isValidityBrithBy15IdCard(idCard15) {
  var year = idCard15.substring(6, 8);
  var month = idCard15.substring(8, 10);
  var day = idCard15.substring(10, 12);
  var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
  // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
  if (temp_date.getYear() != parseFloat(year) ||
    temp_date.getMonth() != parseFloat(month) - 1 ||
    temp_date.getDate() != parseFloat(day)) {
    return false;
  } else {
    return true;
  }
}

// 这个属性是将方法名暴露出来,否则需要引用的页面取不到
module.exports = {
  checkIdCard,
}

在需要引入idcard.js页面
index.wxml

<view class="page-pad">
  <view class="font-size26 color-99">
    为确保您账号的安全及正常使用,依据《网络安全法》相关要求,进行下一步操作需要实名认证,感谢您的理解及支持!
  </view>
  <view class="mar-top30">
    <view class="font-size30 color-29">真实姓名</view>
    <view class="font-size28 color-29 mar-top10">
      <input type="text" placeholder="请填写您的姓名" placeholder-class="placeholder" bindinput="nameInput"></input>
    </view>
  </view>
  <view class="mar-top30">
    <view class="font-size30 color-29">身份证号</view>
    <view class="font-size28 color-29 mar-top10">
      <input type="text" placeholder="请填写您的身份证号" placeholder-class="placeholder" bindinput="cardInput"></input>
    </view>
  </view>
</view>
<view class="sure-btn bg-orange font-size32 color-29" bindtap="sureTap">确认提交</view>

index.json

{
  "navigationBarTitleText": "身份资料",
  "usingComponents": {}
}

index.js

const idcard = require('../../../utils/idcard.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  nameInput(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  cardInput(e) {
    this.setData({
      userCard: e.detail.value
    })
  },
  sureTap() {
    if(!this.data.userName) {
      util.showAllToast('请填写您的姓名', 'none', 2000)
    } else if(!this.data.userCard) {
      util.showAllToast('请填写您的身份证号', 'none', 2000)
    } else if(!idcard.checkIdCard(this.data.userCard)) {
      util.showAllToast('请填写正确的身份证号', 'none', 2000)
    } else {
      util.showAllToast('上传中', 'loading', 2000)
    }
  }
})

index.wxss

page{
  background-color: #ffffff;
}
.sure-btn{
  margin-top: 60rpx!important;
}
上一篇 下一篇

猜你喜欢

热点阅读