user1845163
user1845163

Reputation: 317

How to validate multiple phone number using regular expression

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.

Upvotes: 0

Views: 2426

Answers (4)

Vidhya Sagar Reddy
Vidhya Sagar Reddy

Reputation: 1631

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}

Upvotes: 0

Ramin Asadi
Ramin Asadi

Reputation: 281

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");
        }
    }

Upvotes: 1

Ehsan
Ehsan

Reputation: 32651

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.

Upvotes: 0

insomnium_
insomnium_

Reputation: 1820

  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.

Upvotes: 0

Related Questions