mrblah mrblah - 17 days ago 5
C# Question

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

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).

Answer

Use the expression:

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

ie: using System.Text.RegularExpressions;

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