美文网首页
软键盘并保留两位小数不可输入多个小数点

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

作者: 北上广_d8bd | 来源:发表于2021-12-16 14:31 被阅读0次

此软键盘用的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>

相关文章

网友评论

      本文标题:软键盘并保留两位小数不可输入多个小数点

      本文链接:https://www.haomeiwen.com/subject/gvlvfrtx.html