import { isNull } from '@u/common' // 常用规则 const rules: any = { // 校验空白符 trim, // 零或正整数 zeroOrPositiveInteger: { reg: /^\d+$/, message: '请输入零或正整数' }, // 零或正整数 integer: { reg: /^-?[1-9]\d*$|^0$/, message: '请输入整数' }, // 非负数且保留小数点后两位 negativeNumberAndTwoDecimalPlaces: { reg: /^[0-9]+(\.[0-9]{1,2})?$/, message: '请输入非负数且保留小数点后两位' }, // 非负数且保留小数点后一位 negativeNumberAndOneDecimalPlaces: { reg: /^[0-9]+(\.[5-5]{1,1})?$/, message: '请输入非负数且只能半天或整天' }, // 校验手机号 phone: { reg: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '请输入正确的手机号' }, // 校验URL url: { reg: /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/, message: '请输入正确的URL' }, // 校验邮箱 email: { reg: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, message: '请输入正确的邮箱' }, // 校验大写 upperCase: { reg: /^[A-Z]+$/, message: '请输入正确的大写' }, // 校验小写 lowerCase: { reg: /^[a-z]+$/, message: '请输入正确的小写' }, // 校验身份证 IdCard: { reg: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '请输入正确的身份证号' }, // 英文和数字 englishOrNumerals: { reg: /^[A-Za-z0-9]+$/, message: '请输入英文或数字' } } /** * 表单校验方法(表单用 elementplus模式) * @param validator|arguments[0] 校验(string: rules里匹配,!string:正则 ) * @param message|arguments[1] 错误提示 * @returns 返回表单校验的callback */ export function validator({ validator, message }:any) { validator = validator || arguments[0] message = message || arguments[1] || '' if (typeof validator === 'string') { // 如果传入string格式 // 如果去reg对象找到是对象形式,则代表这是正则,调用validatorReg() if (Object.prototype.toString.call(rules[validator]) === '[object Object]') { if (message) { return validatorTodo({ reg: rules[validator].reg, message: message }) } else { return validatorTodo(rules[validator]) } } else if (Object.prototype.toString.call(rules[validator]) === '[object Function]') { // 如果去reg对象找到是函数,则直接返回该函数 return rules[validator] } } else if (validator instanceof RegExp) { // 如果传入正则格式 message = message || '请输入正确的内容' return validatorTodo({ reg: validator, message: message }) } else if (Object.prototype.toString.call(validator) === '[object Function]') { // 如果传入是函数则直接返回函数 return validator } } // 校验todo export function validatorTodo({ reg, message }:any) { if (!reg) console.warn('undefined reg') return (rule:any, value: any, callback:any) => { if (isNull(value)) return callback() else value = value.toString() if (!reg.test(value)) { return callback(new Error(message)) } else { return callback() } } } // 校验空白符 (校验字符串的头尾空白符,空白符包括:空格、制表符 tab、换行符等其他空白符等) export function trim(rule:any, value: any, callback:any) { if (isNull(value)) { return callback() } value = value.toString() if (value.trim().length === 0) { return callback(new Error('不能只输入空白符')) } else if (value.trim().length !== value.length) { return callback(new Error('不允许输入多余的空白符')) } else { return callback() } } export function validateCode (rule:any, value: any, callback:any) { const reg = /^\d+$/g if (value || value === 0) { value = value.toString() } if (value.length > 30) { return callback(new Error('编号长度最长为30')) } else { return callback() } } // 名称校验 export function validateName (rule:any, value: any, callback:any) { const reg = /^\d+$/g const specialStr = /[\\/:*?"<>|]+/g if (value || value === 0) { value = value.toString() } if (!value) { return callback(new Error('请输入名称')) } else { if (value.length > 32) { return callback(new Error('名称长度最长为32')) } else { if(specialStr.test(value)) { return callback(new Error('名称不能包含 \\ / : * ? " < > | ')) } else { return callback() } } } } export function validatorSortNo (rule:any, value:any, callback:any) { if (Number(value) >= 1000) return callback(new Error('长度不可超过3字符')) else return callback() }