How to prevent invalid characters from being typed into input fields

后端 未结 7 1774
猫巷女王i
猫巷女王i 2020-11-29 10:11

Onkeydown, I run the following JavaScript:

function ThisOnKeyDown(el) {
   if (el.title == \'textonly\') {
       !(/^[A-Za-zÑñ-\\s]*$/i).test(e         


        
7条回答
  •  难免孤独
    2020-11-29 10:36

    $('.key-filter').keypress(function () {
        if (event.key.replace(/[^\w\-.]/g,'')=='') event.preventDefault();
    });
    

    then add the key-filter class to your input if using jquery or

    
    

    just put the charaters you want to allow inside the [] after the ^. this allows all letters numbers _ - and .

提交回复
热议问题