需求:兩個輸入框夫否,一個只能輸入英文和數(shù)字彻犁,另一個只能輸入中文和數(shù)字。
<el-input
placeholder="請輸入角色英文名稱..."
style="width: 300px"
v-model="newRoleNameEn">
<template slot="prepend">ROLE_</template>
</el-input>
<el-input
placeholder="請輸入角色中文名稱..."
style="width: 250px;margin-left: 5px"
v-model="newRoleNameZh">
</el-input>
<el-button type="success" @click="addNewRole" style="margin-left: 5px">添加角色</el-button>
設(shè)置 watch 監(jiān)聽 input 輸入值
// 監(jiān)聽
watch:{
// 監(jiān)聽英文名只能輸入英文和數(shù)字
newRoleNameEn:function(){
this.newRoleNameEn=this.newRoleNameEn.replace(/[\W]/g,'');
},
// 監(jiān)聽中文名只能輸入中文和數(shù)字
newRoleNameZh:function(){
this.newRoleNameZh = this.newRoleNameZh.replace(/[^0-9\u4e00-\u9fa5]/g,'');
}
}
JavaScript 中 replace() 方法
replace() 方法用于在字符串中用一些字符替換另一些字符凰慈,或替換一個與正則表達式匹配的子串汞幢。
語法
stringObject.replace( regexp/substr , replacement )
參數(shù)
regexp/substr :必需。規(guī)定子字符串或要替換的模式的 RegExp 對象溉瓶。
請注意急鳄,如果該值是一個字符串,則將它作為要檢索的直接量文本模式堰酿,而不是首先被轉(zhuǎn)換為 RegExp 對象疾宏。
replacement :必需。一個字符串值触创。規(guī)定了替換文本或生成替換文本的函數(shù)坎藐。
返回值
一個新的字符串,是用 replacement 替換了 regexp 的第一次匹配或所有匹配之后得到的哼绑。