input输入数字且保留两位小数

 <input v-model="formData.mealStandard" type="text" @keyup="validateMealStandard($event)" placeholder="请输入"/>

//金额
validateMealStandard(e){ 
    let mealStandard = e.target.value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '');
     if (mealStandard>=999999999.99){ 
             this.formData.mealStandard = 999999999.99;
         }else { 
             this.formData.mealStandard=mealStandard;
         }
     },
//数字
onlyKeepTwoNumberAfterPoint(type) {  /*限制格式*/
      if(type==1) { 
          let number = (this.formData.number.match(/^\d*(\.?\d{0,0})/g)[0]);
          if (isNaN(number)) { /*非数字*/
              this.formData.number = '';
          } else { 
              if (number == '') { /*为空*/
                  this.formData.number = '';
              } else { /*不为空*/
                  if (number >= 999999999) { 
                      this.formData.number = 999999999;
                  } else { 
                      this.formData.number = number;
                  }
              }
          }
      }
  },

    原文作者:布衣前端
    原文地址: https://blog.csdn.net/qq_41444226/article/details/104511550
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞