current position:Home>Elementui form custom regular expression validation text box

Elementui form custom regular expression validation text box

2022-06-24 08:24:10Brother Nie

combination element Form rule mode , Need to be in data Written in the return of form and formRules, The custom rule written here is to combine formRules, Examples are as follows :

stay data Function to write custom rules , stay rules For use in

data() {
    
    const validatePhone = (rule, value, callback) => {
    
      if (value === '') {
    
        callback(new Error(' Please enter your mobile number '))
      } else {
    
        if (!/^1[3456789]\d{9}$/.test(value)) {
    
          callback(new Error(' Please input the correct mobile number '))
        } else {
    
          callback()
        }
      }
    }
    return {
    
      model: {
    
        username: '',
        nickname: '',
        password: '',
        phone: '',
        email: '',
      },
      rules: {
    
        username: [{
     required: true, message: ' Please enter your account number ', trigger: 'blur' }],
        nickname: [{
     required: true, message: ' Please enter your nickname ', trigger: 'blur' }],
        password: [{
     required: true, message: ' Please input a password ', trigger: 'blur' }],
        phone: [{
     required: true, validator: validatePhone, trigger: 'blur' }],
        email: [{
     required: true, message: ' Please enter email address ', trigger: 'blur' },
        {
     type: 'email', message: ' Please enter the correct email address ', trigger: ['blur', 'change'] }]
      }
    }
  },

copyright notice
author[Brother Nie],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/202206240459169211.html

Random recommended