vue element-ui使用自定義正則表達式:vue
註冊提交按鈕事件:let validatePass = (rule, value, callback) => { let regExp = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/ if (value === '') { callback(new Error('請輸入密碼')) } else if (regExp.test(value) === false) { callback(new Error('8-16字母和數字組成,不能是純數字或純英文')) } else { if (this.ruleForm.repass !== '') { this.$refs.ruleForm.validateField('repass') } callback() } }
submitForm (formName) { this.$refs[formName].validate((valid) => { if (valid) { let _this = this this.$ajax.post('請求地址', _this.ruleForm) .then((response) => { this.$message.success('註冊成功!') this.$refs[formName].resetFields() // 跳轉到登陸頁 this.$router.push({path: '/'}) }) } else { console.log('error submit!!') return false } }) }
首先打印一下this.$refs[formName],檢查是否拿到了正確的須要驗證的form。
其次在拿到了正確的form後,檢查該form上添加的表單驗證是否正確,須要注意的點有:ajax
-
el-form rules,model屬性綁定,ref標識正則表達式
-
el-form-item prop屬性綁定element-ui