SexyMF
SexyMF

Reputation: 11155

C# - Regular Expression for NO numbers allowed

I want to validate first name and last name from all existing languages.
So I want to validate that there are numbers in a string.

Thanks

Upvotes: 2

Views: 5084

Answers (4)

Kevin VDF
Kevin VDF

Reputation: 122

I just tried this one and it should do the trick:

var regex = new Regex(@"[0-9]", RegexOptions.IgnoreCase);
var m = regex.Match(stringValue);
if (m.Success)
    //TODO

Upvotes: 0

Adam
Adam

Reputation: 1264

Just do !Regex if your validation is in a if statement.

  if ( !Regex.Match ( stringToCheck, "^[0-9]+$" ).Success ) {
      // TODO.
  }

Upvotes: 2

FailedDev
FailedDev

Reputation: 26930

Don't even have to use regex:

string tmp = "foo";
var match = tmp.IndexOfAny("0123456789".ToCharArray()) != -1;

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

[\s\p{L}]

would be the correct character class for this. But of course names can contain many more characters than those (how about Tim O'Reilly or William Henry Gates III.?).

See also Falsehoods Programmers Believe About Names.

Upvotes: 6

Related Questions