Regex Replace anything but numbers and lowercase

前端 未结 4 1057
醉梦人生
醉梦人生 2021-01-18 23:29

I have an input which I am binding to keyup()

On each keyup, I want it to:

  1. disallow any characters that are not a number, a letter, or a dash, and
4条回答
  •  离开以前
    2021-01-19 00:18

    The regex for a number, letter or dash is: [-0-9a-z] (to include a literal dash in your character class, specify it as the first character; thereafter it's considered a range operator).

    Try:

    $('.my-input').keyup(function() {this.value = this.value.toLowerCase().replace(/[^-0-9a-z]/g,''); });
    

提交回复
热议问题