所以我有這個輸入標簽:
<input ... onkeypress="return /^[A-Za-z0-9 -]*$/.test(event.key)" id="id_title">
我希望它只接受數字、字母和空格。我想知道如何在我的 JS 檔案中用現代 JS 版本的 jQuery 重寫它,因為所有來自互聯網的舊 JS 方法都被劃掉了。就像在這里一樣,https: //www.codegrepper.com/code-examples/javascript/allow only letters in input javascript onkeypress或keyCode方法現在不起作用。他們被劃掉了。
$('#id_title').on('keydown', function() {
let regex = /^[A-Za-z0-9 -]*$/ // accepts only letters, numbers and spaces
... // how to continue?
}
提前致謝!
uj5u.com熱心網友回復:
試試這個 -
$('#id_title').on('input', function(event) {
let regex = /^[A-Za-z0-9 -]*$/;
return regex.test(event.key);
});
查看有關該方法的MDN 檔案test!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/479826.html
標籤:javascript html jQuery
