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

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

    In order to check if the string is both a combination of letters and digits, you can re-write @jgauffin answer as follows using .NET 4.0 and LINQ:

    if(!string.IsNullOrWhiteSpace(yourText) && 
    yourText.Any(char.IsLetter) && yourText.Any(char.IsDigit))
    {
       // do something here
    }
    

提交回复
热议问题