Jquery: filter input on keypress

前端 未结 5 759
花落未央
花落未央 2020-12-18 04:45

I have a text field, which will accept only the following characters:

Allowed characters: [a-z 0-9 + # - .]

This is the same filter SO

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 05:12

    You don't need to replace the char, only prevent it.

    var char = String.fromCharCode(event.which)
    if (!char.match(/^[^A-Za-z0-9+#\.\-]+$/)) event.preventDefault();
    

提交回复
热议问题