input 限制输入
<input
onkeyup="this.value=this.value.replace(/^\D*([1-9]\d*\.?\d{0,2})?.*$/,'$1')"
onafterpaste="this.value=this.value.replace(/^\D*([1-9]\d*\.?\d{0,2})?.*$/,'$1')"
[maxlength]="10"
placeholder="Other Amount" [(ngModel)]="money" class="input" appTwoDigitDecimaNumber
/>
<input
onkeyup="value.includes('.')?value=(value.replace(/[^\d^\.]+/g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.')).substring(0, value.indexOf('.') + 3):value=value.replace(/[^\d^\.]+/g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.')"
class="input"
[placeholder]="inputTips"
[(ngModel)]="amount"
maxlength="12"
/>
数字处理
changeAmount() {
let str = (this.amount + '');
let num = undefined;
if ((this.amount + '').includes('.')) {
if ((this.amount + '').charAt(0) === '.') {
this.amount =
'0' +
(this.amount + '')
.replace(/[^\d^\.]+/g, '')
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.')
.substring(0, str.indexOf('.') + 3);
}
this.amount = (this.amount + '')
.replace(/[^\d^\.]+/g, '')
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.')
.substring(0, (this.amount + '').indexOf('.') + 3);
if(Number(this.amount) > 0){
for(let i = 0; i < str.length-1; i++){
if(str[i] !== '0'){
num = i;
break;
}
}
this.amount = (this.amount + '').substring(num);
}else{
this.amount = (this.amount + '').replace(/^0+\./g,'0.');
}
} else {
this.amount = (this.amount + '').replace(/^[0]+/, '');
this.amount = (this.amount + '')
.replace(/[^\d^\.]+/g, '')
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.');
}
}
IptBlur(){
this.changeAmount();
if((this.amount+'').charAt((this.amount+'').length-1) === '.'){
this.amount = (this.amount+'').substring(0, (this.amount + '').indexOf('.'));
}else{
if((this.amount + '').includes('.')){
if ((this.amount + '').charAt(0) === '.') {
this.amount =
'0' +
(this.amount + '')
.replace(/[^\d^\.]+/g, '')
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.')
.substring(0, (this.amount + '').indexOf('.') + 3);
}
this.amount = (this.amount + '').replace(/^0+\./g,'0.');
}else{
let str = this.amount + '';
this.amount = str.replace(/^[0]+/, '');
}
}
}