2 回答

TA貢獻1818條經驗 獲得超8個贊
檢查這個
abc.oninput = function() {
const val = this
if (/[0-9~`!@#$%\^&*()+=\-\[\]\\';,/{}|\\":<>\??£.]+/.test(this.value)) {
const i = this.value.match(/[0-9~`!@#$%\^&*()+=\-\[\]\\';,/{}|\\":<>\??£.]+/g)
if (i !== null) {
i.map(function(el) {
val.value = val.value.replace(el, '')
})
}
}
}
<input type="text" data-value-field="value" name="styleName" id="abc" />

TA貢獻1829條經驗 獲得超7個贊
onpaste="return false"用于取消粘貼事件。
autocomplete="off"用于防止自動完成。
setInterval(() => { ... }, 100);用于定期檢查輸入并刪除任何數字或特殊字符。這可以防止用戶在開發者控制臺中使用input.value = " ... "將輸入值設置為無效值,因為輸入將自動更正。
const input = document.getElementById('cleanse');
setInterval(() => {
input.value = input.value.replace(/[^a-zA-Z ]/g, "");
}, 100);
<input type="text" data-value-field="value" name="styleName" onkeypress="return /[a-z]/i.test(event.key)" onpaste="return false" autocomplete="off" id="cleanse" />
- 2 回答
- 0 關注
- 138 瀏覽
添加回答
舉報