My objective is to see if the user puts in a valid phone number once. If not, I just tell them its wrong. I've looked at How to format a string as a telephone number in C# and some others, but im still not sure.
What I need to do, is make sure the user puts in a valid number meaning 1) it can only be numbers 2) the dashes must be in the right place and 3) its the right amount of characters.
I did the 3rd one. And number 1 should just be a TryParse validation.. I am mostly figuring out number 2. Is the above question correct about doing something like:
string phone = i["MyPhone"].ToString();
string area = phone.Substring(0, 3);
string major = phone.Substring(3, 3);
string minor = phone.Substring(6);
string formatted = string.Format("{0}-{1}-{2}", area, major, minor);
Now, this shows .Format. I don't think I need that because I am only supposed to validate if a number is formatted correctly, not to actually put the numbers in between dashes.
So, the end result would be Please enter number: 123-456-7890 Congrats this number is valid
or
Please enter number: 123-45-45-4 This number is not valid.
Avoid using Regex solutions and the number does NOT have to be real. Just as long as it fits the format above.