vue+element項目中過濾輸入框特殊字符小結(jié)
更新時間:2019年08月07日 09:06:31 作者:Thinkguo
這篇文章主要介紹了vue+element項目中過濾輸入框特殊字符小結(jié),本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
可以在main.js中寫入方法
Vue.prototype.validSe = function (value, number = 255) { value = value.replace(/[`~*~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~!@#¥%……&*()——\-+={}|《》?:“”【】、;‘',。、]/g, '').replace(/\s/g, ""); if (value.length >= number) { this.$message({ type: "warning", message: `輸入內(nèi)容不能超過${number}個字符` }); } return value; };
HTML部分
<el-input maxlength='15' :value="searchForm.logId" @input='e => searchForm.logId = validSe (e,15)' placeholder="請輸入日志ID"></el-input>
需要將v-model
拆分為:value和@input
通過以上方法又?jǐn)U展出以下方法
//只能輸漢字 Vue.prototype.chineseOnly = function (value) { value = value.replace(/[^\u4E00-\u9FA5]/g, ''); return value }; //只能輸正整數(shù) Vue.prototype.idOnly = function (value) { value = value.replace(/[^0-9]/g, ''); return value }; //不允許輸漢字 Vue.prototype.noChineseOnly = function (value) { value = value.replace(/[\u4E00-\u9FA5]/g, ''); return value }; //逗號和數(shù)字 Vue.prototype.programIdOnly = function (value) { value = value.replace(/[^0-9,]/g, ''); return value }; //數(shù)字和回車 Vue.prototype.idsOnly = function (value) { value = value.replace(/[^\r\n0-9]/g, ''); return value }; //數(shù)值大小限定 Vue.prototype.numberLimit = function (value) { value = value.replace(/[^0-9]/g, ''); if (value >= 2147483647) { this.$message({ type: "warning", message: `最大可輸入值為2147483647` }); } return value }; // 正整數(shù) Vue.prototype.onlyPositiveInteger = function (value) { value = String(value).match(/[1-9]\d*/g, "") return value === null ? '' : Number(value[0]) }; // 正整數(shù)(包含0) Vue.prototype.onlyPositiveInteger1 = function (value) { console.log(typeof (value)); value = String(value).match(/[1-9]\d*|0/g, "") return value === null ? '' : Number(value[0]) }; // 負(fù)整數(shù) Vue.prototype.onlyNegativeInteger = function (value) { value = String(value).match(/^-[1-9]*\d*/g, "") return value === null ? '' : value[0] === '-' ? '-' : value[0] === '-0' ? '' : Number(value[0]) }; // 負(fù)整數(shù)(包含0) Vue.prototype.onlyNegativeInteger1 = function (value) { value = String(value).match(/^-[1-9]*\d*|0/g, "") return value === null ? '' : value[0] === '-' ? '-' : Number(value[0]) }; // 整數(shù) Vue.prototype.onlyInteger = function (value) { value = String(value).match(/^-?[1-9]*\d*|0/g, '') return value === null ? '' : value[0] === '-' ? '-' : value[0] === '' ? '' : Number(value[0]) }; // 整數(shù)區(qū)間 Vue.prototype.onlySection = function (value, min, max) { if (min < 0) { value = String(value).match(/-?[1-9]*\d*/g, "") } else { value = String(value).match(/[1-9]*\d*/g, "") } // value = String(value).match(/-?[1-9]*\d*/g, "") value = value === null ? '' : value[0] === '-' ? '-' : value[0] === '' ? '' : Number(value[0]) if (value < min) { return min } else if (value > max) { return max } else { return value } };
總結(jié)
以上所述是小編給大家介紹的vue+element項目中過濾輸入框特殊字符小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Vue中computed和watch的區(qū)別小結(jié)
watch和computed都是以Vue的依賴追蹤機制為基礎(chǔ)的,當(dāng)某一個依賴型數(shù)據(jù)發(fā)生變化的時候,所有依賴這個數(shù)據(jù)的相關(guān)數(shù)據(jù)會自動發(fā)生變化,即自動調(diào)用相關(guān)的函數(shù),來實現(xiàn)數(shù)據(jù)的變動,這篇文章簡單介紹下Vue中computed和watch的區(qū)別異同,感興趣的朋友一起看看吧2022-12-12Vue3報錯‘defineProps‘?is?not?defined的解決方法
最近工作中遇到vue3中使用defineProps中報錯,飄紅,所以這篇文章主要給大家介紹了關(guān)于Vue3報錯‘defineProps‘?is?not?defined的解決方法,需要的朋友可以參考下2023-01-01Vue3封裝hooks實現(xiàn)實時獲取麥克風(fēng)音量
這篇文章主要為大家詳細(xì)介紹了Vue3如何通過封裝一個hooks實現(xiàn)實時獲取麥克風(fēng)音量功能,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2024-03-03