只能输入框只能输入正整数,输入同时禁止了以0开始的数字输入,防止被转化为其他进制的数值。
下面为案例:
<!-- 不能输入零时-->
<input type='text' oninput="value=value.replace(/^(0+)|[^\d]+/g,'')">
<!-- 能输入零时-->
<input type='text' oninput="value=value.replace(/^0+(\d)|[^\d]+/g,'')">
<div class="jz_Dialog">
<div>
<div class="saveItem saveTop">
<div class="saveName">结转数量</div>
<el-input v-model="editData.postNum"
class="saveInput"
oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"
@blur="editData.postNum = $event.target.value"
size="mini"></el-input>
</div>
<div class="saveItem saveTop">
<div class="saveName">结转单价</div>
<el-input v-model="editData.postUnitPrice"
class="saveInput"
@input="getNumIpt(editData.postUnitPrice,'1')"
size="mini"></el-input>
<div class="saveItem saveTop">
<div class="saveName">结转金额</div>
<el-input v-model="editData.postPrice"
class="saveInput"
@input="getNumIpt(editData.postPrice,'2')"
size="mini"></el-input>
</div>
</div>
</div>
</div>
getNumIpt (val, type) {
// 非数字 一位小数点 开头不能是小数
if (type == 1) {
this.editData.postUnitPrice = val
.replace(/[^\d.]/g, "")
.replace(/^(\d+)\.(\d*).*$/, "$1.$2")
.replace(/^\.*$/g, "");
}
if (type == 2) {
this.editData.postPrice = val
.replace(/[^\d.]/g, "")
.replace(/^(\d+)\.(\d*).*$/, "$1.$2")
.replace(/^\.*$/g, "");
}
},