AfricasTalkingLtd / africastalking.Net

Africa's Talking API Wrapper for C#
https://developers.africastalking.com
MIT License
18 stars 38 forks source link

Fix Regex.Match in IsPhoneNumber(string number) #20

Closed JobGetabu closed 6 years ago

JobGetabu commented 6 years ago

line: 185 Update to IsPhoneNumber(string number) Regex.Match to accept comma separated numbers +254708440184 :true +254708440184,+254708440184,+254708440184 :true +254708440184, :false +254708440184,+254708440184,+254708440184, :false

TheBeachMaster commented 6 years ago

Hey @JobGetabu ,

Am looking at this Regex, and what it implies is that the phone number can contain a comma, eg +2547,24587654.

Use this site to test your regex

TheBeachMaster commented 6 years ago

This issue can be resolved through this sample

    static void Main(string[] args)
        {
            string phoneNumberList = "+25472487654";
            //string phoneNumberList = "+25472487654,+254734587654,+25471587654";
            string[] nums = phoneNumberList.Split(','); // Split comma separated values
            foreach (string num in nums)
            {
                Console.WriteLine(num);
                Console.WriteLine(IsPhoneNumber(num));
            }

            Console.ReadLine();
        }

        private static bool IsPhoneNumber(string number)
        {
                      return Regex.Match(number, @"^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$").Success && number.Length > 5;
        }
TheBeachMaster commented 6 years ago

[EDIT]

        static void Main(string[] args)
        {
            bool status = false;
            bool stat = true;
            //string phoneNumberList = "+4654";
            string phoneNumberList = "+25472487654,+25473487654,+25471587654,+4654";
            string[] nums = phoneNumberList.Split(',');
            foreach (string num in nums)
            {
                Console.WriteLine(num);
                status =  IsPhoneNumber(num);
                Console.WriteLine(status);
                stat = stat & status;
            }
            Console.WriteLine("Overall: " +stat);
            Console.ReadLine();
        }

        private static bool IsPhoneNumber(string number)
        {
                      return Regex.Match(number, @"^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d{5,}$").Success;
        }