javascript limit text input characters

前端 未结 5 1263
攒了一身酷
攒了一身酷 2020-12-22 06:30

I am wanting to restrict the input characters for a text box to [a-z0-9_-]. However whenever if do this buttons like backspace and the arrow keys don\'t work. I have found s

5条回答
  •  星月不相逢
    2020-12-22 07:34

    You can press any key you like, as long as you keep the value from including anything not in the white-list.

    inputelement.onkeyup=function(e){
      e=e || window.event;
      var who=e.target || e.srcElement;
      who.value= who.value.replace(/[^\w-]+/g,'');
    }
    

提交回复
热议问题