How to Check for a valid full name and valid first name in .Net
To check for valid first name and valid full name using simple c# validation techniques. It also accepts one single quote character.
The code chunk To check for valid first name in c# goes below. I have used some basic built in functions from dotnet. You can customize it as per your requirements.
public bool CheckValidFirstName(string input)
{
bool isValid = true;
int count = 0;
input = input.Trim();
for (int i = 0; i < input.Length; i++)
{
if (!char.IsLetter(input, i))
{
if (input.Length > 1)
{
if (input[i].ToString() == "'" && i < input.Length - 1) //for accepting single quote but not consecutive & not at the end
{
count++;
if (count > 1)
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
}
return isValid;
}
To check for valid full name
public bool CheckValidFullName(string input)
{
bool isValid = true;
int count = 0;
input = input.Trim();
for (int i = 0; i < input.Length; i++)
{
if (!char.IsLetter(input, i))
{
if (input.Length > 1)
{
if (input[i].CompareTo(' ').Equals(0))//to accept whitespace but not consecutive
{
if (input[i - 1].CompareTo(input[i]).Equals(0))
{
isValid = false;
break;
}
}
else if (input[i].ToString() == "'" && i < input.Length - 1)//for accepting single quote but not consecutive and not at the end
{
count++;
if (count > 1)
{
isValid = false;
break;
}
}
else if (input[i].ToString() == "." && i < input.Length - 1)
{
count++;
if (count > 3)
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
}
return isValid;
}