美文网首页
验证方法

验证方法

作者: 与你何干_1bc9 | 来源:发表于2021-10-14 12:05 被阅读0次
    import { gt, gte, lt, lte, eq, isInteger} from "lodash";
    export const validatorInput = (value, props, callback) => {
     const {
       required,
       type = 'string',
       intlength,
       declength,
       plus,
       negative,
       zero,
       max,
       min,
       equal,
       label = '该项',
       maxLabel,
       minLabel,
       polling
     } = props
    
     const dataType = Object.prototype.toString.call(value);
    
     if (required && !value && dataType !== '[object Number]') return callback(new Error(`[${label}]为必填项`))
     if (value === undefined || value === null) return callback()
     if (type === 'string') {
       let str = String(value)
       // if (!isString(value)) return callback(new Error(`[${label}]应为字符串`))
       if (intlength && gt(str.length, intlength)) return callback(new Error(`[${label}]最多输入${intlength}位字符`));
       else return callback()
     }
    
     if (type === 'number') {
       const number = Number(value)
       const [int, dec = ''] = value.toString().split('.')
       if (number === Infinity) return callback(new Error(`请输入有效的数字`))
       if (isNaN(number) || Object.prototype.toString.call(number % 1) !== "[object Number]") return callback(new Error(`请输入正确的数字`))
       if (zero && (value === 0 || value === '0')) return callback()
       if (plus && lte(number, 0)) return callback(new Error(`[${label}]应为正数`))
       if (negative && gte(number, 0)) return callback(new Error(`[${label}]应为负数`))
       if (eq(declength, 0) && !isInteger(number)) return callback(new Error(`[${label}]应为整数`))
       if (intlength) {
         if (int < 0 && gt(int.length, intlength + 1)) return callback(new Error(`[${label}]最多输入${intlength}位整数`))
         if (int >= 0 && gt(int.length, intlength)) return callback(new Error(`[${label}]最多输入${intlength}位整数`))
       }
       if (declength && gt(dec.length, declength)) return callback(new Error(`[${label}]最多输入${declength}位小数`))
       if (max && equal && gt(number, max)) return callback(new Error(`[${label}]的值应小于等于${maxLabel ? maxLabel : max}`))
       if (min && equal && lt(number, min)) return callback(new Error(`[${label}]的值应大于等于${minLabel ? minLabel : min}`))
       if (max && !equal && gte(number, max)) return callback(new Error(`[${label}]的值应小于${maxLabel ? maxLabel : max}`))
       if (min && !equal && lte(number, min)) return callback(new Error(`[${label}]的值应大于${minLabel ? minLabel : min}`))
     }
    
     if (type === 'array') {
       if (dataType !== '[object Array]') return callback(new Error(`[${label}]应为多选项`))
       if (value.length === 0 && required) return callback(new Error(`[${label}]为必填项`))
       if (intlength && gt(value.length, intlength)) return callback(new Error(`[${label}]最多选择${intlength}组数据`))
       else return callback()
     }
    
     if (polling) polling()
     return callback()
    }
    
    
    import { validatorInput } from "@/utils/validator"
    
     price: [
              { trigger: 'blur', validator: (rule, value, callback) => validatorInput(value, { required: false, type: 'number', intlength: 12, declength: 2, label: '价值' }, callback) }
            ],
    

    相关文章

      网友评论

          本文标题:验证方法

          本文链接:https://www.haomeiwen.com/subject/qztwahtx.html