0

I have a scenario, where i have to validate multiple phone numbers at a time. For example i will input phone number like this in the grid.

+46703897733;+46733457773;+46703443832;+42708513544;+91703213815;+919054400407.

Any one help me please?

Thanks in advance.

user1845163
  • 307
  • 1
  • 5
  • 17

4 Answers4

1

use below code for +46... numbers. other number is simlar

 string regexPattern = @"^+46[0-9]{9}$";
    Regex r = new Regex(regexPattern);

    foreach(string s in numbers)
    {
        if (r.Match(s).Success)
        {
            Console.WriteLine("Match");
        }
    }
Ramin Asadi
  • 281
  • 2
  • 17
  • This is very much hard-coded, how do you match with this expression all the numbers supplied by the OP?For every different code (+42, +91 etc) you need to write a separate Regex with this approach. – RahulD Jul 29 '13 at 21:08
  • The code for this example. For all cases, you can use this code @"^+[1-9][0-9]{10}$" – Ramin Asadi Jul 31 '13 at 11:03
0
  1. Choose appropriate and suitable regular expression e.g. from here
  2. Iterate through your number collection and validate them one by one like:

    Regex rgx = new Regex(yourPattern, RegexOptions.IgnoreCase);
    foreach(string num in numbers)
    {
    if(rgx.Matches(num ))
    //do something you need
    }
    

    You also can add RegularExpressionValidator to your phone number column cells in grid and pass it your pattern. Then button click or any event that causes validation will do it for you.

insomnium_
  • 1,800
  • 4
  • 23
  • 39
0

if + is mandatory in your number than do this in c#

        string[] numbers = new string[] { "+46703897733","+46733457773","46733457773"};
         string regexPattern = @"^\+(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$";
        Regex r = new Regex(regexPattern);

        foreach(string s in numbers)
        {
            if (r.Match(s).Success)
            {
               //"+46703897733","+46733457773" are valid in this case
                Console.WriteLine("Match");
            }
        }

if + is not mandatory you can do this

         string regexPattern = @"^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$";
         // all the numbers in the sample above will be considered as valid.
Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

Your regular expression pattern should be then:

[+][1-9][0-9]* 

and this is as of you required; If you want to limit it, like to: +911234567890, then your exp should be:

[+][1-9][0-9]{11,11}
Vidhya Sagar Reddy
  • 1,521
  • 1
  • 13
  • 25