Vue 實現(xiàn)input輸入框只能輸入數(shù)字以及小數(shù)點后兩位

1.調(diào)用 checkInput ,實現(xiàn):?@keyup.native="product.price = checkInputs(product.price)"

<el-input v-model="product.price" class="bg-grey" size="small" ? ?placeholder="實際銷售價格" style="width: 210px;" @keyup.native="product.price = checkInputs(product.price)">

? ? ? ? ? ? <span slot="prefix">¥</span>

? </el-input>

2.實現(xiàn)方法:checkInput

//檢測控制只能輸入小數(shù)點后2位方法

export function checkInput(num) {

? let str = num.toString();

? var len1 = str.substr(0, 1);

? var len2 = str.substr(1, 1);

? //如果第一位是0,第二位不是點,就用數(shù)字把點替換掉

? if (str.length > 1 && len1 == 0 && len2 != ".") {

? ? str = str.substr(1, 1);

? }

? //第一位不能是.

? if (len1 == ".") {

? ? str = "";

? }

? //限制只能輸入一個小數(shù)點

? if (str.indexOf(".") != -1) {

? ? var str_ = str.substr(str.indexOf(".") + 1);

? ? if (str_.indexOf(".") != -1) {

? ? ? str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1);

? ? }

? ? if (str_.length > 2) {

? ? ? this.$message.warning(`金額小數(shù)點后只能輸入兩位,請正確輸入!`);

? ? ? return (str = "");

? ? }

? }

? //正則替換

? str = str.replace(/[^\d^\.]+/g, ""); // 保留數(shù)字和小數(shù)點

? return str;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容