I am trying to make an app where you can add phone numbers and other stuff and I want to check if the number is in correct format so i have been using this loop to check this
int numer = int.Parse(Console.ReadLine());
int count = 0;
int number = numer;
while (number > 0)
{
number /= 10;
count ;
if (number)
{
Console.WriteLine(count);
stringList.Add(nazwa);
intList.Add(numer);
//Console.Clear();
Console.WriteLine($"You added new contact {nazwa} which number is {numer} ");
Console.WriteLine();
}
else
{
Console.WriteLine(count);
//Console.Clear();
Console.WriteLine("This number is in a wrong format!");
Console.WriteLine();
}
}
The problem is when i will type number that has 10 digits program will add this number to the database and after that is going to send error to the user.
CodePudding user response:
To validate it you just need:
string input = Console.ReadLine();
bool valid = input.Length == 10 && input.All(char.IsDigit);
I just saw that you want to validate phone-numbers. Well, then this is not sufficient. But if a phone number is valid depends on the country/region. Search for phone number regex(for example in US).