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

后端 未结 10 1242
执笔经年
执笔经年 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 15:53

    Use the following expression:

    ^[a-zA-Z0-9]*$
    

    ie:

    using System.Text.RegularExpressions;
    
    Regex r = new Regex("^[a-zA-Z0-9]*$");
    if (r.IsMatch(SomeString)) {
      ...
    }
    

提交回复
热议问题