-2

how can i validate user iranian mobile phone number via jQuery in input:text.Iran Mobile phone has numeral system like this:

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

Some example prefixes:

0913894----
0937405----
0935673---- 
0901524---- 
0935673---- 
0920854---- 
0921457---- 
Me hdi
  • 1,832
  • 6
  • 24
  • 35
  • can you show us your code ? or you just waiting for someone to do it for you ? http://stackoverflow.com/questions/6960596/example-of-a-regular-expression-in-jquery-for-phone-numbers – fernando Dec 07 '16 at 12:26
  • According to wikipedia 3'rd digit after 0 will be either 9 or [1-8] for iran numbers. You can create a custom function in JS for number validation – Ravi Roshan Dec 07 '16 at 12:31
  • @Ravi Roshan, How is it? – Me hdi Dec 07 '16 at 12:34
  • You should provide code which you tried to do. or you are waiting for someone to do code as said by @mariusz. – TechnoCrat Dec 07 '16 at 12:39
  • look up javascript's match, and regex in general – A. L Dec 07 '16 at 12:44

2 Answers2

1

You can use Jquery Validation Plugin and add a custom rule

this answer from Andrew Whitaker here provides a good example for what you need. Note that you need to build a regex to match those patterns you need.

Something like

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

for this pattern 0913-xxx-xxxx

Community
  • 1
  • 1
Carlos Crespo
  • 256
  • 1
  • 7
0

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