iview表單驗證之正則驗證、函數驗證


iview表單驗證之正則

正則驗證:

代碼:

loginRules: {
        stringLength: [
          { required: true, message: '該字段不能為空', trigger: 'blur' },
          { pattern: /^[1-9]\d*$/, message: '該字段為整數', trigger: 'blur' }
        ]
}

函數驗證:

1.首先在data中定義驗證方法

data () {
    const validateMax = (rule, value, callback) => {
      if (this.formItem.min !== undefined && this.formItem.max !== undefined && this.formItem.min !== '' && this.formItem.max !== '') {
        if (this.formItem.min * 1 > this.formItem.max * 1) {
          callback(new Error('最大值必須大於最小值'))
          this.$refs.FormRef.validateField('min') // iviewForm方法,驗證min
        } else {
          callback()
          this.$refs.FormRef.validateField('min')
        }
      } else {
        callback()
      }
    }
    const validateMin = (rule, value, callback) => {
      if (this.formItem.min !== undefined && this.formItem.max !== undefined && this.formItem.min !== '' && this.formItem.max !== '') {
        if (this.formItem.min * 1 > this.formItem.max * 1) {
          callback(new Error('最小值必須小於最大值'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
}

2.在表單驗證中插入驗證

/* 表單驗證 */
      loginRules: {
        max: [
          { pattern: /^-?[1-9]\d*$/, message: '該字段只能是整數', trigger: 'blur' },
          { validator: validateMax, trigger: 'blur' }
        ],
        min: [
          { pattern: /^-?[1-9]\d*$/, message: '該字段只能是整數', trigger: 'blur' },
          { validator: validateMin, trigger: 'blur' }
        ]
}

 

 

鑽研不易,轉載請注明出處......

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM