Commit 14f45963 authored by 管新明's avatar 管新明

fix: 调整validateRangeNumber方法校验

parent 8bbc3827
...@@ -28,15 +28,26 @@ export const isNumber = val => { ...@@ -28,15 +28,26 @@ export const isNumber = val => {
* @returns { boolean } * @returns { boolean }
*/ */
export const validateRangeNumber = (number, [min, max] = [], fixed = 0) => { export const validateRangeNumber = (number, [min, max] = [], fixed = 0) => {
const pattern = new RegExp(`^-?[0-9]+(\\.[0-9]{0,${fixed}})?$`); let pattern = new RegExp(`^[0-9]+(\\.[0-9]{1,${fixed}})?$`);
// 判断空字符
if (typeof number === 'string' && !number.trim()) return false
if (fixed === 0 || !number.includes('.')) {
if (min > 0) {
// 校验正整数
pattern = /^[1-9]\d*$/
} else if (min === 0 ) {
// 校验自然数
pattern = /^([1-9]\d*|[0]{1,1})$/
}
}
if (min > max) { if (min > max) {
console.error('数字范围的最小值不能大于最大值'); console.error('数字范围的最小值不能大于最大值');
return; return;
} }
if (number != 0) {
if (!pattern.test(number) || number === '') return false; if (!pattern.test(number)) return false;
}
if (number < min || number > max) return false; if (number < min || number > max) return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment