A string that must be exactly one name and another that must be exactly zero or one name

For FirstName and LastName you can split on ' ' and see that the count is 1.

bool valid = fn.Split(' ').Length == 1

And for the MiddleName let it be also null or empty:

bool valid = string.InNullOrEmpty(mn) || mn.Split(' ').Length == 1

You can add .Trim if you think a space slipped into one of the names in the beginning or end.

bool validFN = fn.Trim().Split(' ').Length == 1
bool validMN = string.InNullOrEmpty(mn) || mn.Trim().Split(' ').Length == 1

Browse More Popular Posts

Leave a Comment