vue如何设置输入框只能输入数字且只能输入小数点后两位,并且不能输入减号

 更新时间:2024年05月24日 10:19:21   作者:沐卿゚  
这篇文章主要介绍了vue如何设置输入框只能输入数字且只能输入小数点后两位,并且不能输入减号问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

vue设置输入框只能输入数字且只能输入小数点后两位,并且不能输入减号

<el-input v-model.trim="sb.price" placeholder="现价" class="input_w3"  oninput="value=value.replace(/[^0-9.]/g,'').replace(/\.{2,}/g,'.').replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')"/>

限制只能输入1到100,且只能输入小数点后二位

<el-input v-model="params.tongZhengPercentage" type="number" placeholder="店铺抽成占比"   oninput="if(!/^[0-9]+$/.test(value)) value=value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); if(value>100)value=100;if(value<0)value=null;if(value<0)value=null;if((value[0] == 0 && value[1] > 0) || value == '00')value=value.slice(1);"/>
								

只能输入1到100

<el-input 
    v-model="num" type="number" placeholder="请输入"   
    oninput="if(!/^[0-9]+$/.test(value)) 
                 value=value.replace(/\D/g,'');
             if(value>100)
                 value=100;
             if(value<0)
                 value=null" 
/>

嘎嘎简单、、、、、、、、、

vue只能输入数字和俩位小数的自定义指令

/**
 * 限制输入框只能输入n为小数
 */
export const inputLimt = {
  bind(el, binding) {
    var wins_0 = /[^\d]/g //整数判断
    var wins_1 = /[^\d^\.]/g //小数判断
    var flag = true;
    var points = 0;
    var lengths = 0
    var remainder = 0
    var no_int = 0
    const target = el instanceof HTMLInputElement ? el : el.querySelector("input");
    target.addEventListener("compositionstart", e => {
      flag = false;
    });
    target.addEventListener("compositionend", e => {
      flag = true;
    });
    target.addEventListener("input", e => {
      setTimeout(function () {
        if (flag) {
          if (binding.value == 0) {
            if (wins_0.test(e.target.value)) {
              e.target.value = e.target.value.replace(wins_0, "");
              e.target.dispatchEvent(new Event("input")) //手动更新v-model值
            }
          }
          if (binding.value == 1) {
            if (wins_0.test(e.target.value.toString().replace(/\d+\.(\d*)/, '$1'))) {
              remainder = true
            }
            if ((e.target.value.split('.')).length - 1 > 1) {
              points = true
            }
            if (e.target.value.toString().split(".")[1] != undefined) {
              if (e.target.value.toString().split(".")[1].length > 1) {
                lengths = true
              }
            }
            if (e.target.value.toString().indexOf(".") != -1) {
              no_int = false
            } else {
              no_int = true
            }
            if (wins_1.test(e.target.value) || lengths || points || remainder) {
              if (!no_int) {
                e.target.value = e.target.value.replace(wins_1, "").replace('.', '$#$').replace(/\./g, '').replace(
                    '$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').substring(0, e.target.value.indexOf(
                    ".") + 2)
              } else {
                e.target.value = e.target.value.replace(wins_0, "")
              }
              e.target.dispatchEvent(new Event("input"))
            }
          }
          if (binding.value == 2) {
            if (wins_0.test(e.target.value.toString().replace(/\d+\.(\d*)/, '$1'))) {
              remainder = true
            }
            if ((e.target.value.split('.')).length - 1 > 1) {
              points = true
            }
            if (e.target.value.toString().split(".")[1] != undefined) {
              if (e.target.value.toString().split(".")[1].length > 2) {
                lengths = true
              }
            }
            if (e.target.value.toString().indexOf(".") != -1) {
              no_int = false
            } else {
              no_int = true
            }
            if (wins_1.test(e.target.value) || lengths || points || remainder) {
              if (!no_int) {
                e.target.value = e.target.value.replace(wins_1, "").replace('.', '$#$').replace(/\./g, '').replace(
                    '$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').substring(0, e.target.value.indexOf(
                    ".") + 3)
              } else {
                e.target.value = e.target.value.replace(wins_0, "")
              }
              e.target.dispatchEvent(new Event("input"))
            }
          }
        }
      }, 0)
    })
  }
}

main.js

Vue.directive('inputLimit',inputLimt)

使用

  <el-input  v-input-limit="2"
                      v-model="popAddOrEditDialog.form.ratio" placeholder="请输入服务比例">
            </el-input>

vue输入框只能输入数字类型,禁止输入和粘贴e

js怎么去除1e里面e 

方法一:使用 Number() 函数将科学计数法表示的字符串转换为数字。然后,使用 toString() 方法将其转换回字符串形式,这样就会自动移除科学计数法中的 "e"

var num = 1e10; // 科学计数法表示的数字
var numStr = Number(num).toString(); // 转换为字符串,自动移除 "e"
console.log(numStr); // 输出 "10000000000"

方法二:使用正则表达式替换方法移除科学计数法中的 "e"。

var num = 1e10; // 科学计数法表示的数字
var numStr = num.toString().replace("e", ""); // 使用 replace 方法替换 "e" 为空字符串 console.log(numStr); // 输出 "10000000000"

vue中限制长度以及数字(包括e) 

// 封装方法--只允许输入正数包 
export function getNum(val) { 
  // 先把非数字的都替换掉,除了数字 
  val = Number(val).toString().replace(/[^\d]/g, '') 
  return val 
}

<el-input
  type="number"
  v-model.trim="ruleForm.height"
  clearable
  placeholder="请输入数字"
  οnkeydοwn="return event.keyCode !== 69"
  @input="changeNumber('height',ruleForm.height,8)"
/>
changeNumber(name, obj, len = 8) {
  const t = obj.length > len ? obj.slice(0, len) : obj
  this.$set(this.ruleForm, name, getNum(t))
}

vue中自动保存两位小数 

// 封装方法--只允许输入数字包含小数点
export function getFloorNumber(val, saveNumber = 2) {
  // 先把非数字的都替换掉,除了数字
  val = val.replace(/[^\d.]/g, '')
  val = val.replace(/\.{2,}/g, '.')
  val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
  const index = val.indexOf('.')
  if (index != -1) {
    const arr = val.split('.')
    if (arr[1].length > saveNumber) {
      arr[1] = arr[1].substr(0, saveNumber)
    }
    val = arr.join('.')
  }
  return val
}
<el-input
  v-model="ruleForm.sharedArea"
  placeholder="请填写面积"
  clearable
  @input="changeSpliceArea('area')"
  @blur="changeArea('area')"
/>
changeSpliceArea(name, len = 8) {
  this.ruleForm[name] = getFloorNumber(this.ruleForm[name])
  if (this.ruleForm[name].length >= len) {
    this.ruleForm[name] = this.ruleForm[name].substr(0, len)
  }
},
changeArea(name) {
  const t = this.ruleForm[name] && this.ruleForm[name].charAt(this.ruleForm[name].length - 1)
  if (t == '.') {
    this.ruleForm[name] += '00'
  }
}

常用工具方法

// 只允许输入数字包含负数
export function getNumber(val) {
  const t = val.charAt(0)
  // 先把非数字的都替换掉,除了数字
  val = val.replace(/[^\d]/g, '')
  // 如果第一位是负号,则允许添加
  if (t === '-') {
    val = '-' + val
  }
  return val
}

// 只允许输入正数包
export function getNum(val) {
  // 先把非数字的都替换掉,除了数字
  val = Number(val).toString().replace(/[^\d]/g, '')
  return val
}

// 只允许输入数字包含小数点
export function getFloorNumber(val, saveNumber = 2) {
  // 先把非数字的都替换掉,除了数字
  val = val.replace(/[^\d.]/g, '')
  val = val.replace(/\.{2,}/g, '.')
  val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
  const index = val.indexOf('.')
  if (index != -1) {
    const arr = val.split('.')
    if (arr[1].length > saveNumber) {
      arr[1] = arr[1].substr(0, saveNumber)
    }
    val = arr.join('.')
  }
  return val
}
// 只允许输入数字包含小数,不限制长度
export function getFloorNumNoLength(val) {
  const t = val.charAt(0)
  if (t === '.') {
    return val.replace('.', '')
  }
  val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
  // 先把非数字的都替换掉,除了数字
  val = val.replace(/[^\d.]/g, '')
  return val
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue+LogicFlow+Flowable实现工作流

    Vue+LogicFlow+Flowable实现工作流

    本文主要介绍了Vue+LogicFlow+Flowable实现工作流,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-12-12
  • 阿望教你用vue写扫雷小游戏

    阿望教你用vue写扫雷小游戏

    这篇文章主要介绍了阿望教你用vue写扫雷小游戏,本文通过实例代码效果图展示的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • vue中v-model和响应式的实现原理解析

    vue中v-model和响应式的实现原理解析

    这篇文章主要介绍了vue中v-model和响应式的实现原理,通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • vue主动刷新页面及列表数据删除后的刷新实例

    vue主动刷新页面及列表数据删除后的刷新实例

    今天小编就为大家分享一篇vue主动刷新页面及列表数据删除后的刷新实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • vue实现瀑布流组件滑动加载更多

    vue实现瀑布流组件滑动加载更多

    这篇文章主要为大家详细介绍了vue实现瀑布流组件滑动加载更多,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • uniapp微信小程序webview和h5数据通信代码示例

    uniapp微信小程序webview和h5数据通信代码示例

    这篇文章主要介绍了uniapp微信小程序webview和h5数据通信的相关资料,文章还列出了项目的结构,包括微信小程序和h5端的主要文件和组件,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-01-01
  • Vue分页组件实现过程详解

    Vue分页组件实现过程详解

    Web应用程序中资源分页不仅对性能很有帮助,而且从用户体验的角度来说也是非常有用的。在这篇文章中,将了解如何使用Vue创建动态和可用的分页组件
    2022-12-12
  • vue下载excel文件的四种方法实例

    vue下载excel文件的四种方法实例

    最近vue项目中遇到了需求,客户端需要实现以excel表格形式下载数据,这篇文章主要给大家介绍了关于vue下载excel文件的四种方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • vue中的双向数据绑定原理与常见操作技巧详解

    vue中的双向数据绑定原理与常见操作技巧详解

    这篇文章主要介绍了vue中的双向数据绑定原理与常见操作技巧,结合实例形式详细分析了vue中双向数据绑定的概念、原理、常见操作技巧与相关注意事项,需要的朋友可以参考下
    2020-03-03
  • Vue中Vue.extend()的使用及解析

    Vue中Vue.extend()的使用及解析

    这篇文章主要介绍了Vue中Vue.extend()的使用及解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10

最新评论