iview表單驗證之正則iview
正則驗證:函數
代碼:ui
loginRules: { stringLength: [ { required: true, message: '該字段不能爲空', trigger: 'blur' }, { pattern: /^[1-9]\d*$/, message: '該字段爲整數', trigger: 'blur' } ] }
函數驗證:this
1.首先在data中定義驗證方法spa
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.在表單驗證中插入驗證code
/* 表單驗證 */ 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' } ] }
鑽研不易,轉載請註明出處......orm