简体   繁体   中英

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.

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");
        }
    }
  1. Choose appropriate and suitable regular expression eg 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.

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.

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM