Reputation: 11155
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
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
Reputation: 1264
Just do !Regex if your validation is in a if statement.
if ( !Regex.Match ( stringToCheck, "^[0-9]+$" ).Success ) {
// TODO.
}
Upvotes: 2
Reputation: 26930
Don't even have to use regex:
string tmp = "foo";
var match = tmp.IndexOfAny("0123456789".ToCharArray()) != -1;
Upvotes: 2
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