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

此软键盘用的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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容