Giter Site home page Giter Site logo

常用正则收集 about blog HOT 4 OPEN

zhouzhili avatar zhouzhili commented on July 23, 2024
常用正则收集

from blog.

Comments (4)

zhouzhili avatar zhouzhili commented on July 23, 2024

只能输入零和非零开头的数字

^(0|[1-9][0-9]*)$

from blog.

zhouzhili avatar zhouzhili commented on July 23, 2024

国内手机号验证

^1(3|4|5|6|7|8|9)\d{9}$

from blog.

zhouzhili avatar zhouzhili commented on July 23, 2024

身份证号码校验方法

/**
 * 校验身份证是否合法
 * @param {String} no 身份证号码字符串
 */
function validateIdNo(no) {
  if (no.length < 18) {
    return {
      status: 'error',
      msg: '身份证号码长度不能小于18位'
    }
  }
  // 最后一位的校验
  const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  const marks = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  let sum = 0
  for (let i = 0; i < 17; i++) {
    sum += Number(no[i]) * weights[i]
  }
  const lastCode = marks[sum % 11]
  if (lastCode === no[no.length - 1]) {
    return {
      status: 'success',
      msg: '身份证号码校验通过'
    }
  }
  return {
    status: 'error',
    msg: '身份证号码校验不通过'
  }
}

from blog.

zhouzhili avatar zhouzhili commented on July 23, 2024

银行卡校验方法

/**
 * 银行卡Luhn校验算法
 * https://en.wikipedia.org/wiki/Luhn_algorithm
 * @param {String} bankCardNo 银行卡号
 */
 function validateBankCardNo(bankCardNo) {
  // 字符串化并去除中间或者前后的空格
  const strDigits = `${bankCardNo}`.replace(/[\D]/g, '')
  // 银行卡号必须为12-19位数字
  if (!/^\d{12,19}$/.test(strDigits)) {
    return {
      status: 'error',
      msg: '银行卡号码长度需在12-19位之间'
    }
  }
  let sum = 0
  const len = strDigits.length
  for (let i = len - 1; i > -1; i--) {
    const digit = parseInt(strDigits[i], 10) * ((len - i) % 2 === 0 ? 2 : 1)
    if (digit < 10) {
      sum += digit
    } else {
      sum += (digit % 10) + parseInt(digit / 10, 10)
    }
  }
  if (sum % 10 === 0) {
    return {
      status: 'success',
      msg: '银行卡号码校验通过'
    }
  } else {
    return {
      status: 'error',
      msg: '银行卡号码校验不通过'
    }
  }
}

from blog.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.