【问题标题】:jQuery validate for mobile phone numbers?jQuery验证手机号码?
【发布时间】:2016-12-07 12:22:56
【问题描述】:

如何通过 jQuery 在input:text.Iran 中验证用户伊朗手机号码。伊朗手机的数字系统如下:

091- --- ----
093[1-9] --- ----
092[1-9] --- ----
090[1-9] --- ----

一些示例前缀:

0913894----
0937405----
0935673---- 
0901524---- 
0935673---- 
0920854---- 
0921457---- 

【问题讨论】:

  • 你能告诉我们你的代码吗?或者你只是在等待有人为你做这件事? stackoverflow.com/questions/6960596/…
  • 根据维基百科,0 之后的第 3 位数字将是 9 或 [1-8] 用于伊朗号码。您可以在JS中创建一个自定义函数进行数字验证
  • @Ravi Roshan,怎么样?
  • 您应该提供您尝试执行的代码。或者您正在等待某人按照@mariusz 所说的那样编写代码。
  • 查找 javascript 的匹配项和一般的正则表达式

标签: javascript jquery


【解决方案1】:

您可以使用 Jquery Validation Plugin 并添加自定义规则

来自Andrew Whitaker 的这个答案为您提供了一个很好的例子。请注意,您需要构建一个正则表达式来匹配您需要的那些模式。

类似

09\d{2}\-\d{3}-\d{4}

对于这个模式 0913-xxx-xxxx

【讨论】:

    【解决方案2】:

    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 +۹۸ (۹۱۵) ۸۰ ۸۰ ۸۸۸"));
    console.log(getMobiles("9135561548"));
    console.log(getMobiles("09131261548"));
    console.log(getMobiles("+989131261548"));
    console.log(getMobiles("+19132264758"));
    console.log(getMobiles("981234567896547896541236542332"));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2017-02-06
      • 1970-01-01
      • 2013-07-01
      • 2013-12-20
      • 2018-03-02
      • 2015-10-08
      相关资源
      最近更新 更多