vue添加验证文件 验证身份证号码是否正确

1.创建验证文件 IdCard.js

// Generated by CoffeeScript 1.12.7

/*
 Validid is open source in:
 https://github.com/Edditoria/validid
 under MIT license:
 https://github.com/Edditoria/validid/blob/master/LICENSE.md
 */

(function() {
  var Validid, validid;

  Validid = (function() {
    function Validid() {}

    Validid.prototype.tools = {
      normalize: function(id) {
        var re;
        re = /[-\/\s]/g;
        id = id.toUpperCase().replace(re, '');
        re = /\([A-Z0-9]\)$/;
        if (re.test(id)) {
          id = id.replace(/[\(\)]/g, '');
        }
        return id;
      },
      isDateValid: function(idDate, minDate, maxDate) {
        var isFormatValid, parseDate;
        if (minDate == null) {
          minDate = 'default';
        }
        if (maxDate == null) {
          maxDate = 'today';
        }
        if (minDate === 'default' || minDate === '') {
          minDate = '18991129';
        }
        isFormatValid = function(date) {
          return typeof date === 'string' && /^[0-9]{8}$/.test(date);
        };
        if (!isFormatValid(idDate)) {
          return false;
        }
        if (!isFormatValid(minDate)) {
          return false;
        }
        parseDate = function(input) {
          var date, day, isDayValid, isFutureDate, isLeapYear, isMonthValid, maxDay, month, startIndex, year;
          startIndex = 0;
          year = +input.substring(startIndex, startIndex += 4);
          month = input.substring(startIndex, startIndex += 2);
          day = +input.substring(startIndex, startIndex += 2);
          date = new Date(year, +month - 1, day);
          maxDay = '01,03,05,07,08,10,12'.indexOf(month) >= 0 ? 31 : '04,06,09,11'.indexOf(month) >= 0 ? 30 : (isLeapYear = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0), isLeapYear ? 29 : 28);
          isDayValid = day > 0 && day <= maxDay;
          if (!isDayValid) {
            return false;
          }
          isMonthValid = +month > 0 && +month <= 12;
          if (!isMonthValid) {
            return false;
          }
          isFutureDate = new Date() < date;
          if (isFutureDate) {
            return false;
          }
          return date;
        };
        idDate = parseDate(idDate);
        if (idDate === false) {
          return false;
        }
        minDate = parseDate(minDate);
        if (minDate === false) {
          return false;
        }
        maxDate = maxDate === 'today' ? new Date() : typeof maxDate === 'string' ? parseDate(maxDate) : maxDate;
        if (maxDate === false) {
          return false;
        }
        return (idDate >= minDate) && (idDate <= maxDate);
      },
      getMaxDate: function(yearsOld) {
        var now, year;
        now = new Date();
        year = now.getFullYear() - yearsOld;
        return new Date(year, now.getMonth(), now.getDate());
      }
    };

    Validid.prototype.cnid = function(id) {
      var isChecksumValid, isDateValid, isFormatValid, isLengthValid;
      isLengthValid = function(id) {
        return id.length === 18;
      };
      isFormatValid = function(id) {
        return /^[0-9]{17}[0-9X]$/.test(id);
      };
      isDateValid = (function(_this) {
        return function() {
          return _this.tools.isDateValid(id.substring(6, 14), '18860625');
        };
      })(this);
      isChecksumValid = function(id) {
        var char, checkDigit, getWeight, i, identifier, index, len, remainder, weightedSum;
        identifier = id.slice(0, -1);
        checkDigit = id.slice(-1) === 'X' ? 10 : +id.slice(-1);
        getWeight = function(n) {
          return Math.pow(2, n - 1) % 11;
        };
        weightedSum = 0;
        index = id.length;
        for (i = 0, len = identifier.length; i < len; i++) {
          char = identifier[i];
          weightedSum += +char * getWeight(index);
          index--;
        }
        remainder = (12 - weightedSum % 11) % 11 - checkDigit;
        return remainder === 0;
      };
      id = this.tools.normalize(id);
      return isLengthValid(id) && isFormatValid(id) && isDateValid() && isChecksumValid(id);
    };

    Validid.prototype.twid = function(id) {
      var isChecksumValid, isFormatValid, isLengthValid;
      isLengthValid = function(id) {
        return id.length === 10;
      };
      isFormatValid = function(id) {
        return /^[A-Z][12][0-9]{8}$/.test(id);
      };
      isChecksumValid = function(id) {
        var char, i, idLen, idTail, len, letterIndex, letterValue, letters, remainder, weight, weightedSum;
        idLen = id.length;
        letters = 'ABCDEFGHJKLMNPQRSTUVXYWZIO';
        letterIndex = letters.indexOf(id[0]) + 10;
        letterValue = Math.floor(letterIndex / 10) + (letterIndex % 10) * (idLen - 1);
        idTail = id.slice(1);
        weight = idLen - 2;
        weightedSum = 0;
        for (i = 0, len = idTail.length; i < len; i++) {
          char = idTail[i];
          weightedSum += +char * weight;
          weight--;
        }
        remainder = (letterValue + weightedSum + +id.slice(-1)) % 10;
        return remainder === 0;
      };
      id = this.tools.normalize(id);
      return isLengthValid(id) && isFormatValid(id) && isChecksumValid(id);
    };

    Validid.prototype.hkid = function(id) {
      var getLetterValue, isChecksumValid, isFormatValid, isLengthValid, isLetter;
      getLetterValue = function(letter) {
        return letter.charCodeAt(0) - 64;
      };
      isLetter = function(char) {
        return /[a-zA-Z]/.test(char);
      };
      isLengthValid = function(id) {
        return id.length === 8 || id.length === 9;
      };
      isFormatValid = function(id) {
        return /^[A-MP-Z]{1,2}[0-9]{6}[0-9A]$/.test(id);
      };
      isChecksumValid = function(id) {
        var char, charValue, checkDigit, i, identifier, len, remainder, weight, weightedSum;
        weight = id.length;
        weightedSum = 0;
        identifier = id.slice(0, -1);
        checkDigit = id.slice(-1) === 'A' ? 10 : +id.slice(-1);
        for (i = 0, len = identifier.length; i < len; i++) {
          char = identifier[i];
          charValue = isLetter(char) ? getLetterValue(char) : +char;
          weightedSum += charValue * weight;
          weight--;
        }
        remainder = (weightedSum + checkDigit) % 11;
        return remainder === 0;
      };
      id = this.tools.normalize(id);
      return isLengthValid(id) && isFormatValid(id) && isChecksumValid(id);
    };

    Validid.prototype.krid = function(id) {
      var isChecksumValid, isDateValid, isFormatValid, isLengthValid;
      isLengthValid = function(id) {
        return id.length === 13;
      };
      isFormatValid = function(id) {
        return /^[0-9]{13}$/.test(id);
      };
      isDateValid = (function(_this) {
        return function(id) {
          var date, maxDate, sDigit, yearPrefix;
          sDigit = id.substring(6, 7);
          yearPrefix = (function() {
            switch (sDigit) {
              case '1':
              case '2':
              case '5':
              case '6':
                return '19';
              case '3':
              case '4':
              case '7':
              case '8':
                return '20';
              default:
                return '18';
            }
          })();
          date = yearPrefix + id.substring(0, 6);
          maxDate = _this.tools.getMaxDate(17);
          return _this.tools.isDateValid(date, 'default', maxDate);
        };
      })(this);
      isChecksumValid = function(id) {
        var char, i, index, len, remainder, weight, weightedSum;
        weight = [2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 0];
        weightedSum = 0;
        index = 0;
        for (i = 0, len = id.length; i < len; i++) {
          char = id[i];
          weightedSum += +char * weight[index];
          index++;
        }
        remainder = (11 - weightedSum % 11) % 10 - +id.slice(-1);
        return remainder === 0;
      };
      id = this.tools.normalize(id);
      return isLengthValid(id) && isFormatValid(id) && isDateValid(id) && isChecksumValid(id);
    };

    return Validid;

  })();

  validid = new Validid();

  if ((typeof module !== "undefined" && module !== null) && module.exports) {
    module.exports = validid;
  }

  if (typeof window !== "undefined" && window !== null) {
    window.validid = validid;
  }

}).call(this);

2.在main.js中引用

import IdCard from '../static/js/IdCard'
Vue.prototype.validid = IdCard;

3.使用

this.validid.cnid('123456789123456789');

参考
https://blog.csdn.net/qq_37330613/article/details/84973877

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容

  • 1说说JVM垃圾回收机制。 http://blog.csdn.net/xiajian2010/article/de...
    Alfred泉阅读 747评论 0 2
  • 本系列出于AWeiLoveAndroid的分享,在此感谢,再结合自身经验查漏补缺,完善答案。以成系统。 Java基...
    济公大将阅读 1,519评论 1 6
  • 返回上一页: 点击回到顶部 解决办法: a标签跳转锚点到页面指定位置 https://blog.csdn.net/...
    zlf_j阅读 1,302评论 0 1
  • 小时侯, 妈妈的爱藏在怀抱里, 抱着它,暖哄哄的。 上学后, 妈妈的爱藏在书包里, 背着它,沉甸甸的。 离家后, ...
    半随烟雨半浮生阅读 389评论 0 2
  • 2. {/六年前 你和段宜恩从高一就认识,那时候大家都很青涩,不敢和对方告白。你看着他觉得他很帅,他看着你觉得你很...
    沙特阿菠蘿阅读 859评论 0 1