INameCreationService.IsValidName(String) Method

Definition

Gets a value indicating whether the specified name is valid.

C#
public bool IsValidName(string name);

Parameters

name
String

The name to validate.

Returns

true if the name is valid; otherwise, false.

Examples

The following code example provides an example INameCreationService.IsValidName method implementation. The method uses a string validation scheme that examines each character of the specified string to determine whether the specified string is a valid name. The method returns true if the string is valid, or false otherwise.

C#
// Returns whether the specified name contains 
// all valid character types.
public bool IsValidName(string name)
{            
    for(int i = 0; i < name.Length; i++)
    {
        char ch = name[i];
        UnicodeCategory uc = Char.GetUnicodeCategory(ch);
        switch (uc) 
        {
            case UnicodeCategory.UppercaseLetter:       
            case UnicodeCategory.LowercaseLetter:     
            case UnicodeCategory.TitlecaseLetter:                                                  
            case UnicodeCategory.DecimalDigitNumber:                         
                break;
            default:
                return false;                
        }
    }
    return true;        
 }

Remarks

An implementation of the INameCreationService can have rules that define the parameters for valid names. This method can be implemented to validate a name and enforce those rules.

Applies to

Tuote Versiot
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1