软键盘并保留两位小数不可输入多个小数点

2021-12-16  本文已影响0人  北上广_d8bd

此软键盘用的vant

<template>
<van-number-keyboard v-model="inputPrice" :extra-key="['00', '.']" :show="showKeyboard" maxlength="16" @blur="showKeyboard = false" theme="custom" close-button-text="完成" />
</template>
<script>
export default {
name:'',
data(){
  inputPrice:'', // 输入金额
}
},
watch:{
// 校验输入金额 最多两位小数 最多一个小数点
  inputPrice: {
    handler: function (newVal, oldVal) {
      let n = (newVal.split('.')).length - 1;
      this.inputPrice = this.inputPrice.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
      if (n > 1 || (n == 1 && newVal.length == 1)) {
        this.inputPrice = oldVal
      }
    },
  },
}
</script>
<style type="text/less" lang="less" scoped>
</style>
上一篇下一篇

猜你喜欢

热点阅读