正则表达式 核对input内容,手机号码、中文姓名、车牌号

时间:2025-04-08 11:44:14

在js文件中

编写核对函数
核对每个input是否都填写完整

// 核对input是否填完整
export const checkInput = (inputValue) => {
  for (var i = 0; i < inputValue.length; i++) {
    if (inputValue[i].value === '' || inputValue[i].value === undefined || inputValue[i].value == null) {
      return false
    } else {
      return true
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

核对手机号是否为11位,切由13[0-9]、14[5,7,9]、154、18[0-9]、17[0,1,3,5,6,7,8]开头

// 核对手机号格式
export const checkPhone = (phone) => {
  const reg = /^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\d{8}$/
  return reg.test(phone)
}
  • 1
  • 2
  • 3
  • 4
  • 5

核对中文姓名

// 核对中文姓名格式
export const checkName = (name) => {
  const reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/
  return reg.test(name)
}
  • 1
  • 2
  • 3
  • 4
  • 5

核对车牌号码


// 核对车牌号格式
export const checkLicencePlate = (licencePlate) => {
  const reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/
  return reg.test(licencePlate)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

vue文件中

引入核对函数,并使用

import {checkInput, checkName, checkPhone} from '../assets/js/commonJS'
  • 1
submitClick () {
  var inputValue = document.getElementsByTagName('input')
  console.log(inputValue)
  if (!checkInput(inputValue)) {
    alertshow('请填写完整')
  } else if (!checkName(this.realName)) {
    alertshow('请填写正确真实姓名')
  } else if (!checkPhone(this.phone)) {
    alertshow('请填写正确手机号码')
  } else {
    // 提交函数
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13