Alphanumeric, dash and underscore but no spaces regular expression check JavaScript

前端 未结 7 654
忘掉有多难
忘掉有多难 2020-12-23 10:48

Trying to check input against a regular expression.

The field should only allow alphanumeric characters, dashes and underscores and should NOT allow spaces.

7条回答
  •  时光取名叫无心
    2020-12-23 11:32

    You shouldn't use String.match but RegExp.prototype.test (i.e. /abc/.test("abcd")) instead of String.search() if you're only interested in a boolean value. You also need to repeat your character class as explained in the answer by Andy E:

    var regexp = /^[a-zA-Z0-9-_]+$/;
    

提交回复
热议问题