支付金额输入控制代码
·Shinji用于商家出示静态收款码,用户扫码自主输入金额进行支付,或在应用内充值、收款等功能限制使用自定义键盘输入金额等业务场景。
Source code
function inputValidate(val: string): string {
if (val === '.' || val === '0.') {
return '0.';
}
if (!Number.isNaN(Number(val))) {
const dotIndex = val.indexOf('.');
if (dotIndex !== -1) {
const decimalLength = val.substring(dotIndex + 1).length;
return decimalLength > 2 ? val.substring(0, dotIndex + 3) : val;
}
return val;
}
return val;
}
export const moneyKeyboard = {
input: function (value: string, amount: string): string {
if (amount.includes('.') && value === '.') {
return amount;
}
if (/^0+$/.test(amount) && value === '.') {
return '0.';
}
const val = amount + value;
return inputValidate(val);
},
backspace: function (amount: string): string {
return amount.slice(0, -1);
},
};