abbott-methods
Version:
abbott,methods,method,functions,function
35 lines (33 loc) • 1.13 kB
text/typescript
import { formatAllInt } from '../format/formatAllInt'
import { typeEmpty } from '../type/typeEmpty'
/**
* @desc 验证是否数字格式
* @param {Boolean} required
* @param {String} validateValue
* @param {*} callback
* @param {String} field
* @param {String} [action]
* @param {Number} [min]
* @param {Number} [max]
*/
export const vAllInt = (required: boolean, validateValue: string, callback: any, field: string, action: string = '填写', min?: number, max?: number): void => {
if (!required && !validateValue) {
callback()
} else {
if (typeEmpty(validateValue)) {
action = action || '填写'
callback(new Error(`${field}必须${action}`))
} else {
if (formatAllInt(validateValue)) {
if (min && max && (validateValue.length < ~~Math.abs(min) || validateValue.length > ~~Math.abs(max))) {
const howMany = min === max ? String(max) : min + '-' + max
callback(new Error(`${field}的长度为${howMany}个字符`))
} else {
callback()
}
} else {
callback(new Error(`${field}必须是全整数`))
}
}
}
}