How to Check for a valid names and valid names with number in .Net c#


Check if the entered name is valid or not with and without numbers and accept one white space(not consecutive) and accept one single quote using simple c# validations techniques.

To check for a Valid Name without having numbers but allowing whitespace and single quote.


public bool CheckValidName(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 & not at the end
{
count++;
if (count > 1)
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
}
return isValid;
}



To check for a Valid Name with numbers but not at the beginning.


public bool CheckValidNameWithNumbers(string input)
{
bool isValid = true;
input = input.Trim();
for (int i = 0; i < input.Length; i++)
{
if (!char.IsLetter(input, i))
{
if (i != 0)// to see tat the names do not begin with numbers
{
if (!char.IsNumber(input, i))
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
}
return isValid;
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: