form form verification, calibration

One.onkeyup 

Restricted to numbers only

<el-input

  style="width: 70%"

  οninput="if(value>2147483647)value='2147483647'"

  οnkeyup="value=value.replace(/[^\d]/g,'')"

  placeholder="Please enter the number of daily occupants of the place"

  :readonly="readonly"

/>

Two.pattern

aaWxpUnitPostcode: [{ pattern: /^[1 - 9]\d{5}(?!\d)$/g, required: false, message: 'Please enter the correct postcode', trigger: 'blur' }] ,

3. validator method

confirmPassword: [ 
  { required: true, trigger: "blur", message: "Please enter your password again" }, { 
  required: true, validator: equalToPassword , trigger: "blur" } 
],
const equalToPassword = (rule, value, callback) => { 
  if (this.registerForm.password !== value) { 
    callback(new Error("The passwords entered twice are inconsistent")); 
  } else { 
    callback(); 
  } 
};

Four. js calibration

js can calibrate mobile phone number, ID number, email address, URL, etc. Here is just an example

Put it in validate.js under utils
// Validate the URL

export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

use

import { validURL } from '@/utils/validate'
if (validURL(URL)) { 
  console.log("is the correct URL") 
}

Note that if you need to verify the array, change the prop to the array to be verified, and add type: 'code'

<el-form-item prop="userIdList" label="选择所属库管员" style="display: block">
  <el-select v-model="form.userIdList" multiple >
    <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId"></el-option>
  </el-select>
</el-form-item> 

userIdList: [{ type: 'array' ,required: false, message: 'Please fill in the librarian', trigger: 'blur' }],

Some special symbols regular expressions 

Spaces cannot be entered: ^[^\s]*$

Pure spaces cannot be entered: ^[^\s]+[\s]*.*$

You can enter letters (including uppercase and lowercase): ^[A-Za-z]*$

Contains capital letters and [ , \ , ] , ^ , - , : ^[AZ]*$

You can enter numbers: ^[0-9]*$

Chinese characters can be input: ^[\u4e00-\u9fa5]*$

Guess you like

Origin blog.csdn.net/and_life/article/details/131231579