【问题标题】:Regular expression for Iranian mobile phone numbers?伊朗手机号码的正则表达式?
【发布时间】:2011-07-20 12:13:37
【问题描述】:

如何使用正则表达式验证手机号码? 伊朗手机的数字系统是这样的:

091- --- ----
093[1-9] --- ----

前缀的一些例子:

0913894----
0937405----
0935673---- 
0912112----

来源:http://en.wikipedia.org/wiki/Telephone_numbers_in_Iran

【问题讨论】:

    标签: c# regex validation


    【解决方案1】:

    检测伊朗手机号码的最佳正则表达式

    我确信它是检测伊朗手机号码的最佳正则表达式。

    (0|\+98)?([ ]|-|[()]){0,2}9[1|2|3|4]([ ]|-|[()]){0,2}(?:[0-9]([ ]|-|[()]){0,2}){8}
    

    在javascript中使用

    var
    mobileReg = /(0|\+98)?([ ]|-|[()]){0,2}9[1|2|3|4]([ ]|-|[()]){0,2}(?:[0-9]([ ]|-|[()]){0,2}){8}/ig,
    junkReg = /[^\d]/ig,
    persinNum = [/۰/gi,/۱/gi,/۲/gi,/۳/gi,/۴/gi,/۵/gi,/۶/gi,/۷/gi,/۸/gi,/۹/gi],
    num2en = function (str){
      for(var i=0;i<10;i++){
        str=str.replace(persinNum[i],i);
      }
      return str;
    },
    getMobiles = function(str){
      var mobiles = num2en(str+'').match(mobileReg) || [];
      mobiles.forEach(function(value,index,arr){
        arr[index]=value.replace(junkReg,'');
        arr[index][0]==='0' || (arr[index]='0'+arr[index]);
      });
      return mobiles;
    };
    
    // test
    console.log(getMobiles("jafang 0 91 2 (123) 45-67 jafang or +۹۸ (۹۱۵) ۸۰ ۸۰ ۸۸۸"));
    

    支持所有类似的选项

    912 123 4567
    912 1234 567
    912-123-4567
    912 (123) 4567
    9 1 2 1 2 3 4 5 6 7
    9 -1 (2 12))3 45-6 7
    and all with +98 or 0
    +989121234567
    09121234567
    9121234567
    

    甚至是波斯数字

    +۹۸ (۹۱۵) ۸۰ ۸۰ ۸۸۸
    

    并且只检测真正的伊朗运营商编号 091x 092x 093x 094x

    更多信息:https://gist.github.com/AliMD/6439187

    【讨论】:

    • 一个 (+) 仅用于 jafang
    • 我认为@Mostafa 的回答更好。当您想从网络上的一些抓取文本中提取手机号码时,此方法可能很有用,但是当您处理用户输入时,没有人会输入奇怪的东西(或 jafang ;)
    • @nima 它不适用于波斯数字和 +98** 和 +98 915 (123) *** 以及许多其他...
    • 我知道并且我赞成您的回答。我试图说明的一点是,这种方法并不适合所有人,也不是每个人都需要这么复杂。
    • ([ ]|-|[()]) 最好写成[- ()][1|2|3|4] 匹配管道字符。您的正则表达式匹配 0--9|()1-(1-(1-(1-(1-(1-(1-(1-( 不确定这是一个有效的电话号码。
    【解决方案2】:

    根据维基页面http://en.wikipedia.org/wiki/Telephone_numbers_in_Iran#Mobile_phones,匹配项是:

    091x-xxx-xxxx
    0931-xxx-xxxx
    0932-xxx-xxxx
    0933-xxx-xxxx
    0934-xxx-xxxx
    0935-xxx-xxxx
    0936-xxx-xxxx
    0937-xxx-xxxx
    0938-xxx-xxxx
    0939-xxx-xxxx
    

    看起来像

    (the specific sequence 09) (1 followed by 0-9 OR 3 followed by 1-9) (7 digits) 
    

    假设你现在不关心破折号,这转化为

    09 (1 [0-9] | 3 [1-9]) [0-9]{7} <-- spaces added for emphasis
    09(1[0-9]|3[1-9])[0-9]{7} <-- actual regex
    

    (..|..) 表示 OR,[0-9]{7} 表示完全匹配 7 位数字,...)

    如果你想在指定位置使用破折号:

    09(1[0-9]|3[1-9])-?[0-9]{3}-?[0-9]{4}
    

    应该匹配

    【讨论】:

      【解决方案3】:

      简单是生活的美化。试试这个:

      ^09[0|1|2|3][0-9]{8}$
      //091 for Hamrahe-Aval Operator
      //092 for Rightel Operator
      //093 | 090 for Irancel Oprator 
      

      这是我在项目中经常使用的 c# 扩展方法:

      public static bool IsValidMobileNumber(this string input)
      {
          const string pattern = @"^09[0|1|2|3][0-9]{8}$";
          Regex reg = new Regex(pattern);
          return reg.IsMatch(input);
      }
      

      这样使用:

      If(!txtMobileNumber.Text.IsValidMobileNumber())
      {
        throw new Exception("Mobile number is not in valid format !");
      }
      

      【讨论】:

      • 这个简单是个笑话:P 也不适用于 0903
      【解决方案4】:

      这个怎么样?

      ^(\+98?)?{?(0?9[0-9]{9,9}}?)$

      为:

      09036565656

      09151331685

      09337829090

      09136565655

      09125151515

      +989151671625

      +9809152251148

      check out the results here

      【讨论】:

        【解决方案5】:

        ^09\d{2}\s*?\d{3}\s*?\d{4}$

        这也将允许数字之间有空格(如果它们是 4-3-4)。

        【讨论】:

          【解决方案6】:

          首先让我们为数字编写正则表达式

          ^09\d{9}$ 表示必须以 09 开头,后跟 9 位数字

          或者你可以使用 ^09[0-9]{9}$ 代替 \d 之后

          Regex objNotNaturalPattern=new Regex("[^09[0-9]{9}]");
          

          objNotNaturalPattern.IsMatch(yourPhoneNumber);

          希望这会有所帮助。

          【讨论】:

            【解决方案7】:

            在javascript中你可以使用这个代码

            let username = 09407422054
            let checkMobile = username.match(/^0?9[0-9]{9}$/)
            

            它会返回

            ["9407422054", index: 0, input: "9407422054", groups: undefined]
            

            【讨论】:

              【解决方案8】:

              最好的正则表达式模式

              (+?98|098|0|0098)?(9\d{9})

              【讨论】:

                【解决方案9】:

                \A09\d{9}应该为你做这件事

                【讨论】:

                  【解决方案10】:

                  /^({?(09)([1-3]){2}-([0-9]){7,7}}?)$/

                  匹配:

                  - 0913-1234567
                  - 0933-1234567
                  

                  不匹配:

                  - 0944-1234567
                  - 0955-1234567
                  

                  如果您不想要“-”,只需将其从正则表达式中删除

                  【讨论】:

                    【解决方案11】:

                    要支持 0903---- Irancell 手机号码,请使用:

                    ^(0|\+98)?([ ]|,|-|[()]){0,2}9[0|1|2|3|4]([ ]|,|-|[()]){0,3}(?:[0-9]([ ]|,|-|[()]){0,2}){8}$
                    

                    【讨论】:

                    【解决方案12】:
                    ^(\+98|0)?9\d{9}$
                    

                    将与 +989123456789 和

                    等数字匹配
                    ^[0][9][0-9][0-9]{8,8}$
                    

                    匹配像 09123456789 这样的数字

                    【讨论】:

                      【解决方案13】:
                      string sPattern = "^09[-.\\s]?\\d{2}[-.\\s]?\\d{3}[-.\\s]?\\d{4}$";
                      
                      if (System.Text.RegularExpressions.Regex.IsMatch(stringToMatch, sPattern)){
                         System.Console.WriteLine("Phone Number is valid");
                      } else {
                         System.Console.WriteLine("Phone Number is invalid");
                      }
                      

                      上面的表达式将匹配任何以 09 开头的数字,后跟空格、点或破折号(如果存在),然后是 2 个数字等

                      例如:09001234567 或 09 11 123 4567 或 09-01-123-4567 或 09.01.123.4567 或 0900.000.0000 或 0900-000-0000 或 0900.000-0000 等

                      【讨论】:

                        【解决方案14】:

                        ^09(1\d|3[1-9])-\d{3}-\d{4}$

                        是你需要的正则表达式:

                        从字符串的开头开始(所以没有其他数字潜入)

                        09 是常数

                        1 可以和后面的数字组合,3 不能和 0 组合

                        -,3位数字

                        -,4位数字

                        字符串结尾(同样,没有其他数字潜入)

                        ...但不要相信我的话,运行它(我建议添加更多测试编号):

                                List<string> possible = new List<string> { "091-555-8888", 
                        "0915-888-4444", 
                        "0930-885-8844", 
                        "0955-888-8842" };
                        
                                string regex = @"09(1\d|3[1-9])-\d{3}-\d{4}$";
                        
                                foreach (string number in possible)
                                {
                                    Console.WriteLine("{0} is{1} an Iranian number", number, Regex.IsMatch(number, regex) ? "" : " not");
                                }
                        

                        【讨论】:

                          【解决方案15】:

                          这很简单。 任何格式的伊朗 TCI 电话

                          支持这些格式

                          +98217777777

                          98217777777

                          098217777777

                          0098217777777

                          217777777

                          0217777777

                          function IsIranPhone(phone) {
                              if (phone == null || phone ==undifined) {
                                  return false;
                              }
                              return new RegExp("^((|0|98|098|0098|\\+98)[1-8][1-9][2-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])$").test(phone);
                          }
                          

                          【讨论】:

                            【解决方案16】:

                            在 C# 中使用此代码

                              public bool IsMobileValid(string mobile)
                                {
                                    if (string.IsNullOrWhiteSpace(mobile))
                                        return false;
                                    if (Regex.IsMatch(mobile, @"(0|\+98)?([ ]|-|[()]){0,2}9[1|2|3|4]([ ]|-|[()]) 
                                         {0,2}(?:[0-9]([ ]|-|[()]){0,2}){8}"))
                                        return true;
                                    return false;
                                }
                            

                            【讨论】:

                              【解决方案17】:
                              //check phone number
                              function check_phone(number) {
                                  var regex = new RegExp('^(\\+98|0)?9\\d{9}$');
                                  var result = regex.test(number);
                              
                                  return result;
                              }
                              
                              $('#PhoneInputId').focusout(function () {
                                  var number = $('#crt-phone').val();
                                  if (!check_phone(number)) {
                                     your massage...
                                  } else {
                                     your massage...
                                  }
                              });
                              

                              这是最好的答案,我测试了所有的答案,但这很简单,也是最好的

                              【讨论】:

                                猜你喜欢
                                • 2022-01-12
                                • 1970-01-01
                                • 1970-01-01
                                • 2018-06-05
                                • 2014-04-18
                                • 1970-01-01
                                • 1970-01-01
                                • 2023-02-14
                                相关资源
                                最近更新 更多