Vue:自定义验证commonValidator.js

本文详细探讨了在Vue中创建自定义验证器`commonValidator.js`的方法,涵盖了如何定义验证规则、集成到Vue实例以及在表单验证中使用自定义验证器的关键步骤。通过这个实践教程,读者将能掌握自定义验证器在提高应用数据验证灵活性方面的重要性。
摘要由CSDN通过智能技术生成
/**
 * 自定义校验
 */
import moment from 'moment'

const CommonValidator = {
   
  data() {
   
    return {
   
      countReg: /^\d{
   1,}?$/,
      moneyReg: /^\d{
   1,}?(\.\d{
   0,2})?$/
    }
  },
  methods: {
   
    /**
     * input:验证是金额
     */
    validateMoneyVal(rule, value, callback) {
   
      if (value === undefined || value === null || value === '') {
   
        callback()
        return
      }
      if (value) {
   
        if (!this.moneyReg.test(value)) {
   
          callback(new Error(this.$t('common.validate.insertDecimals')))
        } else if (value > 99999.99) {
   
          callback(new Error(this.$t('common.validate.max99999_99')))
        } else {
   
          callback()
        }
      } else {
   
        return true
      }
    },

    /**
     * input:验证是金额可以为负数
     */
    validateNegativeMoneyReg(rule, value, callback) {
   
      if (value === undefined || value === null || value === '') {
   
        callback()
        return
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
index.vue:202 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'type') at _callee$ (index.vue:202:1) at tryCatch (regeneratorRuntime.js:44:1) at Generator.eval (regeneratorRuntime.js:125:1) at Generator.eval [as next] (regeneratorRuntime.js:69:1) at asyncGeneratorStep (asyncToGenerator.js:3:1) at _next (asyncToGenerator.js:22:1) at eval (asyncToGenerator.js:27:1) at new Promise (<anonymous>) at eval (asyncToGenerator.js:19:1) at VueComponent.handleNodeClick (index.vue:228:1) _callee$ @ index.vue:202 tryCatch @ regeneratorRuntime.js:44 eval @ regeneratorRuntime.js:125 eval @ regeneratorRuntime.js:69 asyncGeneratorStep @ asyncToGenerator.js:3 _next @ asyncToGenerator.js:22 eval @ asyncToGenerator.js:27 eval @ asyncToGenerator.js:19 handleNodeClick @ index.vue:228 handleSizeChange @ index.vue:191 invokeWithErrorHandling @ vue.runtime.esm.js:3971 invoker @ vue.runtime.esm.js:1188 invokeWithErrorHandling @ vue.runtime.esm.js:3971 Vue.$emit @ vue.runtime.esm.js:2874 handleChange @ element-ui.common.js:982 invokeWithErrorHandling @ vue.runtime.esm.js:3971 invoker @ vue.runtime.esm.js:1188 invokeWithErrorHandling @ vue.runtime.esm.js:3971 Vue.$emit @ vue.runtime.esm.js:2874 handleOptionSelect @ select.js:1945 invokeWithErrorHandling @ vue.runtime.esm.js:3971 Vue.$emit @ vue.runtime.esm.js:2874 dispatch @ emitter.js:29 selectOptionClick @ option.js:383 click @ option.js:230 invokeWithErrorHandling @ vue.runtime.esm.js:3971 invoker @ vue.runtime.esm.js:1188 original_1._wrapper @ vue.runtime.esm.js:7265 index.vue:201
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值