web123456

Regular expressions Check input content, mobile phone number, Chinese name, license plate number

In js file

Write a check function
Check whether each input is filled in intact

// Check whether the input is complete
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

Check whether the mobile phone number is 11 digits, start from 13[0-9], 14[5,7,9], 154, 18[0-9], 17[0,1,3,5,6,7,8]

// Check the mobile phone number format
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

Check Chinese name

// Check the Chinese name format
export const checkName = (name) => {
  const reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/
  return reg.test(name)
}
  • 1
  • 2
  • 3
  • 4
  • 5

Check license plate number


// Check the license plate number format
export const checkLicencePlate = (licencePlate) => {
  const reg = /^[Beijing, Tianjin, Shanghai, Chongqing, Hebei, Henan, Yunnan, Liaoning, Hunan, Anhui, Shandong, New Jiangsu, Zhejiang, Jiangxi, Hubei, Guizhou, Gansu, Shanxi, *, Shaanxi, Guizhou, Guangdong, Qinghai, Tibet, Sichuan, Ningqiong Envoy A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9 School Police, * and Macau]{1}$/
  return reg.test(licencePlate)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

existvueIn the file

Introduce verificationfunctionand use

import {checkInput, checkName, checkPhone} from '../assets/js/commonJS'
  • 1
submitClick () {
  var inputValue = document.getElementsByTagName('input')
  console.log(inputValue)
  if (!checkInput(inputValue)) {
    alertshow('Please fill in the complete')
  } else if (!checkName(this.realName)) {
    alertshow('Please fill in the correct real name')
  } else if (!checkPhone(this.phone)) {
    alertshow('Please fill in the correct mobile phone number')
  } else {
    // Submit function
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13