none
Verify a specific phone number RRS feed

  • Question

  • how to write C# code  method will verify phone numbers in accordance with the North American Numbering Plan (NANP) rules.
    Friday, May 11, 2018 12:42 PM

All replies

  • Have you tried a 3rd party library search using Bing or Google or maybe an open source library search.

    Surely, there is source code or a library that already has the functionality out there if you look.

       
    Saturday, May 12, 2018 7:46 AM
  • Hi Hakimeh,

    For your requirement, you could try "Regex", and I suggest you try code below:

    string userInput = txtPhoneNumber.Text;
     
    Regex regexPhoneNumber = new Regex(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");
     
    if (regexPhoneNumber.IsMatch(userInput))
    {
        string formattedPhoneNumber = regexPhoneNumber.Replace(userInput, "($1) $2-$3");
    }
    else
    {
        // Invalid phone number
    }

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 14, 2018 3:29 AM
  • All you have to do is search using Bing or Google.

    https://www.codeproject.com/Tips/477851/Telephone-Validation-Method

    <copied>

    The function provided below validates a telephone against the requirements of the North American Number Plan (NANP) as administered by Neustar, Inc.

    <end>

    Yeah, I wrote programs for Cincinnati Bell. :) 

    Monday, May 14, 2018 3:59 PM