How can I validate a string to only allow alphanumeric characters in it?

后端 未结 10 1241
执笔经年
执笔经年 2020-12-12 15:06

How can I validate a string using Regular Expressions to only allow alphanumeric characters in it?

(I don\'t want to allow for any spaces either).

10条回答
  •  感动是毒
    2020-12-12 16:13

    ^\w+$ will allow a-zA-Z0-9_

    Use ^[a-zA-Z0-9]+$ to disallow underscore.

    Note that both of these require the string not to be empty. Using * instead of + allows empty strings.

提交回复
热议问题