首页 > 其他 > 详细

input输入框价格限定:只能输入正数,小数点后两位,不出现多个小数点,0开头后面不接数字(vue做的)

时间:2019-11-12 15:11:32      阅读:534      评论:0      收藏:0      [点我收藏+]

1.给input绑定keyup函数:

<input
                type="text"
                class="form-control"
                id="inputEmail3"
                v-model="price"
                placeholder="请输入价格"
               @keyup="priceFormat"
              />
 
2.编写priceFormat函数:
priceFormat(){
         //非数字和小数点去掉     
        this.price=this.price.replace(/\D^./,"")
        //防止无输入无限个“.”
        this.price=this.price.replace(/\.+/ ,".")
        //在不是“0.”开头的字符进行修改:“01”=>1
        if(this.price.charAt(0)=="0"&&this.price.charAt(1)!="."&&this.price.length>=2){
          this.price=this.price.replace(/0/ ,"")
        }
        //获取第一个小数点的索引值
        var index=this.price.indexOf(‘.‘)
        //获取最后一个小数点的索引值
        var lastIndex=this.price.lastIndexOf(‘.‘)
        //判断小数点是不是开头,如果是开头,则去除
        if(index==0){
          this.price=this.price.replace(/\./ ,"")
        }
        //只允许小数点后面有2位字符
        if(index>=1){
          this.price=this.price.substring(0,index+3)          
        }
        //防止小数点后面又出现小数点
        if(index!=lastIndex){
          this.price=this.price.substring(0,index+2) 
        }
           
    }

input输入框价格限定:只能输入正数,小数点后两位,不出现多个小数点,0开头后面不接数字(vue做的)

原文:https://www.cnblogs.com/Toulachi/p/11841287.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!