vue限制文本輸入框只允許輸入字母、數(shù)字、禁止輸入特殊字符
一、基本結(jié)構(gòu)
<input type="text" v-model="note" maxlength="18">
<script>
export default {
data () {
return {
note: '',
}
}
}
</script>二、監(jiān)聽表單輸入的內(nèi)容
(1) 只允許輸入字母
watch: {
note (newValue, oldValue) {
console.log(newValue)
this.note = newValue.replace(/[\u4e00-\u9fa5/\s+/]|[`~!@#$%^&*() \\+ =<>?"{}|, \\/ ;' \\ [ \] ·~!@#¥%……&*()—— \\+ ={}|《》?:“”【】、;‘',。、_.-:]/g, "")
}
},(2)只能輸入漢字、英文、數(shù)字
watch: {
note (newValue, oldValue) {
console.log(newValue)
this.note = newValue.replace(/[^a-zA-Z0-9\u4E00-\u9FA5]/g, "")
}
},(3) 只允許輸入數(shù)字
watch: {
note (newValue, oldValue) {
console.log(newValue)
this.note = newValue.replace(/[^\d]/g, "")
}
},(4)只允許輸入數(shù)字、字母
watch: {
note (newValue,oldValue) {
console.log(newValue)
this.note = newValue.replace(/[\u4e00-\u9fa5/\s+/]|[^a-zA-Z0-9\u4E00-\u9FA5]/g, "")
}
},注意 沒有采用添加 input事件
三、使用方法的缺陷:
<input type="text" v-model="note" maxlength="18" @input="filter">
methods: {
filter (e) {
console.log(e)
this.note = e.data.replace(/[\u4e00-\u9fa5/\s+/]|[`~!@#$%^&*() \\+ =<>?"{}|, \\/ ;' \\ [ \] ·~!@#¥%……&*()—— \\+ ={}|《》?:“”【】、;‘',。、_.-:]/g, "")
}
},(1)只能輸入一個字母內(nèi)容

(2)采用輸入法輸入多個字符時會報錯 而且用拼音輸入法按回車鍵也可以顯示其他數(shù)字或字符

補(bǔ)充 校驗(yàn)手機(jī)號
test () {
console.log(/^1[34578]\d{9}$/.test(this.mobile))
}只允許輸入數(shù)字、點(diǎn) 保留兩位小數(shù)
checkNumber(e) {
let val = e.target.value.replace(/(^\s*)|(\s*$)/g, "")
var reg = /[^\d.]/g
// 只能是數(shù)字和小數(shù)點(diǎn),不能是其他輸入
val = val.replace(reg, "")
// // 保證第一位只能是數(shù)字,不能是點(diǎn)
val = val.replace(/^\./g, "");
// // 小數(shù)只能出現(xiàn)1位
val = val.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
// // 小數(shù)點(diǎn)后面保留2位
val = val.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
// console.log(val);
this.$nextTick(() => {
this.form.price = val;
})
},到此這篇關(guān)于vue限制文本輸入框只允許輸入字母、數(shù)字、不允許輸入特殊字符的文章就介紹到這了,更多相關(guān)vue限制文本輸入框輸入數(shù)字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue刷新頁面以后丟失store的數(shù)據(jù)問題
這篇文章主要介紹了解決vue刷新頁面以后丟失store的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue+vite創(chuàng)建項目關(guān)于vite.config.js文件的配置方法
Vue項目創(chuàng)建時,我們見過vue-cli 創(chuàng)建項目和webpack 創(chuàng)建項目等方式,這篇文章主要介紹了Vue+vite創(chuàng)建項目關(guān)于vite.config.js文件的配置方法,需要的朋友可以參考下2023-06-06
在vue中,v-for的索引index在html中的使用方法
下面小編就為大家分享一篇在vue中,v-for的索引index在html中的使用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Vue3+PDF.js實(shí)現(xiàn)高清PDF預(yù)覽組件開發(fā)(移動端)
本文基于Vue3和PDF.js封裝了一套移動端PDF預(yù)覽組件,其核心功能包括高清渲染,雙指手勢縮放,按鈕縮放等內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2026-05-05
Vue技巧Element Table二次封裝實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Vue技巧Element Table二次封裝實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
快速了解Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法
這篇文章主要介紹了Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

