How To Only Allow Alpha Numeric Chars With JavaScript

前端 未结 3 1397
花落未央
花落未央 2020-12-01 23:40

Been playing around with JavaScript, and what Im trying to do is only allow certain characters in the pass word field - a-z, A-Z and 0-9.

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 00:27

    if (name.match(/[\W_]/)) { //...
    

    Meaning if the "name" string has any character which is a non-alphanumeric or an underscore then execute the block. Note that we have to separately check for underscore (_) because the alphanumeric character class (\w) includes the underscore (so the negative class (\W) does not).

提交回复
热议问题