How to check phone numbers on c#?
<p>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 <a href="https://stackoverflow.com/questions/188510/how-to-format-a-string-as-a-telephone-number-in-c-sharp">How to format a string as a telephone number in C#</a> and some others, but im still not sure.</p>

<p>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.</p>

<p>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: </p>

<pre><code>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);
</code></pre>

<p>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. </p>

<p>So, the end result would be
Please enter number: 123-456-7890
Congrats this number is valid</p>

<p>or</p>

<p>Please enter number: 123-45-45-4
This number is not valid.</p>

<p>Avoid using Regex solutions and the number does NOT have to be real. Just as long as it fits the format above.</p>