js实现金额数字输入规范限制(支持负数的输入,精确到小数点后两位【限制范围为:-99999999.99~99999999.99】)

xiaoxiao2025-04-30  17

// 输入事件 keyinput() { let t = this.adjustForm.adjustCash.charAt(0); if(t != '-' && this.adjustForm.types === 1){ // 如果类型里显示为余额,输入的值变为正数时做类型清空,其他情况不做清空 this.adjustForm.types = ''; } this.adjustForm.adjustCash = this.adjustForm.adjustCash.replace(/[^\d\.]/g,""); if(String(t) =='-' && this.adjustForm.adjustCash.charAt(0)!='-'){ this.adjustForm.adjustCash = '-'+this.adjustForm.adjustCash; } let index = this.adjustForm.adjustCash.indexOf('.'); if(index != -1){ this.adjustForm.adjustCash = this.adjustForm.adjustCash.substring(0,index+3); } }, // 失焦事件 keyblur() { let num = this.adjustForm.adjustCash; if(num === '') return; let v = parseInt(this.adjustForm.adjustCash)+''; if(String(num).indexOf('-') == 0 || String(num).indexOf('+') == 0){ v=v.replace(/^[\-\+]+/,''); } if(!/^[+-]?\d*\.?\d{1,3}$/.test(num)) { this.adjustForm.adjustCash = '' this.$message({ message: '请输入正确的数字', duration: 3000, type: 'warning' }); }else if(v.length > 8) { this.adjustForm.adjustCash = '' this.$message({ message: '整数部分不能超过8位', duration: 3000, type: 'warning' }); }else { this.adjustForm.adjustCash = (+num).toFixed(2) } if(!/^[+-]?\d*\.?\d{1,3}$/.test(num)){ this.adjustForm.adjustCash = ''; this.$message({ message: '请输入正确的数字', duration: 3000, type: 'warning' }); } }
转载请注明原文地址: https://www.6miu.com/read-5029507.html

最新回复(0)