input 针对价格输入限制
priceFormat (e) {
this.unitPrice = (this.unitPrice.match(/^\d*(\.?\d{0,2})/g)[0]) || null
//在不是“0.”开头的字符进行修改:“01”=>1
if (this.unitPrice.charAt(0) == "0" && this.unitPrice.charAt(1) != "." && this.unitPrice.length >= 2) {
this.unitPrice = this.unitPrice.replace(/0/, "")
}
if (isNaN(this.unitPrice)) {
this.unitPrice = ''
}
}
priceFormat () {
//非数字和小数点去掉
this.unitPrice = this.unitPrice.replace(/\D^./, "")
//防止无输入无限个“.”
this.unitPrice = this.unitPrice.replace(/\.+/, ".")
//在不是“0.”开头的字符进行修改:“01”=>1
if (this.unitPrice.charAt(0) == "0" && this.unitPrice.charAt(1) != "." && this.unitPrice.length >= 2) {
this.unitPrice = this.unitPrice.replace(/0/, "")
}
//获取第一个小数点的索引值
var index = this.unitPrice.indexOf('.')
//获取最后一个小数点的索引值
var lastIndex = this.unitPrice.lastIndexOf('.')
//判断小数点是不是开头,如果是开头,则去除
if (index == 0) {
this.unitPrice = this.unitPrice.replace(/\./, "")
}
//只允许小数点后面有2位字符
if (index >= 1) {
this.unitPrice = this.unitPrice.substring(0, index + 3)
}
//防止小数点后面又出现小数点
if (index != lastIndex) {
this.unitPrice = this.unitPrice.substring(0, index + 2)
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。