新增营销中心限时折扣模板
This commit is contained in:
44
utils/index.js
Normal file
44
utils/index.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 过滤输入,只允许数字和最多两位小数
|
||||
* @param {string} value - 输入框当前值
|
||||
* @param {boolean} isIntegerOnly - 是否只允许正整数(无小数点),开启时最小值为1,或为传入的值
|
||||
* @returns {string} 过滤后的合法值
|
||||
*/
|
||||
export function filterNumberInput(value, isIntegerOnly = false) {
|
||||
// 第一步就过滤所有非数字和非小数点的字符(包括字母)
|
||||
let filtered = value.replace(/[^\d.]/g, "");
|
||||
|
||||
// 整数模式处理
|
||||
if (isIntegerOnly !== false) {
|
||||
// 移除所有小数点
|
||||
filtered = filtered.replace(/\./g, "");
|
||||
|
||||
// 处理前导零
|
||||
filtered = filtered.replace(/^0+(\d)/, "$1") || filtered;
|
||||
|
||||
// 空值处理(允许临时删除)
|
||||
if (filtered === "") {
|
||||
return "";
|
||||
}
|
||||
|
||||
// 最小值限制
|
||||
if (filtered === isIntegerOnly || parseInt(filtered, 10) < isIntegerOnly) {
|
||||
return isIntegerOnly;
|
||||
}
|
||||
|
||||
return filtered;
|
||||
}
|
||||
|
||||
// 小数模式处理
|
||||
const parts = filtered.split(".");
|
||||
if (parts.length > 1) {
|
||||
filtered = parts[0] + "." + (parts[1].substring(0, 2) || "");
|
||||
}
|
||||
|
||||
// 处理前导零
|
||||
if (filtered.startsWith("0") && filtered.length > 1 && !filtered.startsWith("0.")) {
|
||||
filtered = filtered.replace(/^0+(\d)/, "$1");
|
||||
}
|
||||
|
||||
return filtered;
|
||||
}
|
||||
Reference in New Issue
Block a user