Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I validate mobile numbers with a regular expression? Iran Mobile phones have numeral system like this:

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

Some examples for prefixes:

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

Source: http://en.wikipedia.org/wiki/Telephone_numbers_in_Iran

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
412 views
Welcome To Ask or Share your Answers For Others

1 Answer

Best Regular Expression for Detect Iranian Mobile Phone Numbers

I'm sure its best regular expression for detect iranian mobile number.

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

use in 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 +?? (???) ?? ?? ???"));

support all option like these

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

or even persian numbers

+?? (???) ?? ?? ???

and only detect true iranian operator numbers 091x 092x 093x 094x

for more info : https://gist.github.com/AliMD/6439187


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...