📜  onkeypress 避免输入特殊字符 - 任何代码示例

📅  最后修改于: 2022-03-11 14:57:33.237000             🧑  作者: Mango

代码示例1
$('input').on('keypress', function (event) {
    var regex = new RegExp("^[a-zA-Z0-9]+$");
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
       event.preventDefault();
       return false;
    }
});