Including a hyphen in a regex character bracket?

前端 未结 6 503
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 03:34
$.validator.addMethod(\'AZ09_\', function (value) { 
    return /^[a-zA-Z0-9.-_]+$/.test(value); 
}, \'Only letters, numbers, and _-. are allowed\');
6条回答
  •  自闭症患者
    2020-11-22 04:07

    Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:

    /^[a-zA-Z0-9._-]+$/
    

提交回复
热议问题