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
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
Simplicity is the beautify of life . Try this one :
^09[0|1|2|3][0-9]{8}$
//091 for Hamrahe-Aval Operator
//092 for Rightel Operator
//093 | 090 for Irancel Oprator
Here is an Extension method in c# which I use regularly in my project :
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);
}
Using like this :
If(!txtMobileNumber.Text.IsValidMobileNumber())
{
throw new Exception("Mobile number is not in valid format !");
}
According to the wiki page http://en.wikipedia.org/wiki/Telephone_numbers_in_Iran#Mobile_phones, the matches are:
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
which looks like
(the specific sequence 09) (1 followed by 0-9 OR 3 followed by 1-9) (7 digits)
Assuming you dont care about the dashes for now, this translates to
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
(the (..|..)
does OR, the [0-9]{7}
says match exactly 7 digits, ...)
If you want dashes in the specified location:
09(1[0-9]|3[1-9])-?[0-9]{3}-?[0-9]{4}
should match
How about this one ?
^(\+98?)?{?(0?9[0-9]{9,9}}?)$
working for:
09036565656
09151331685
09337829090
09136565655
09125151515
+989151671625
+9809152251148
check out the results here
^(\+98|0)?9\d{9}$
Will match with numbers like +989123456789 and
^[0][9][0-9][0-9]{8,8}$
match with numbers like 09123456789
^09\d{2}\s*?\d{3}\s*?\d{4}$
This will also allow spaces between numbers (if they are 4-3-4).
First of all lets write the reg ex for the numbers
^09\d{9}$
which means it must start with 09 and followed by 9 digits
or instead of \d you can use ^09[0-9]{9}$ after that
Regex objNotNaturalPattern=new Regex("[^09[0-9]{9}]");
objNotNaturalPattern.IsMatch(yourPhoneNumber);
Hope this helps.
in javascript you can use this code
let username = 09407422054
let checkMobile = username.match(/^0?9[0-9]{9}$/)
it will return
["9407422054", index: 0, input: "9407422054", groups: undefined]
the best regex pattern
(+?98|098|0|0098)?(9\d{9})
\A09\d{9}
should do it for you
To support the 0903---- Irancell mobile numbers, use:
^(0|\+98)?([ ]|,|-|[()]){0,2}9[0|1|2|3|4]([ ]|,|-|[()]){0,3}(?:[0-9]([ ]|,|-|[()]){0,2}){8}$
I used this for Iran Mobile and worked for Irancell , rightell and MCI 0935-0939 ,0912,0901,0910,0902,0903,...
public static MobilePattern: string = '09[0-9]{9}$';
Regex:
/^(\{?(09)([1-3]){2}-([0-9]){7,7}\}?)$/
Matches:
- 0913-1234567
- 0933-1234567
Doesn't match:
- 0944-1234567
- 0955-1234567
If you don't want the '-', simply remove it from the regex
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");
}
The expression above will match any number starting with 09 followed by a space, dot, or dash if present, then 2 digits, etc
ex: 09001234567 or 09 11 123 4567 or 09-01-123-4567 or 09.01.123.4567 or 0900.000.0000 or 0900-000-0000 or 0900.000-0000 etc
^09(1\d|3[1-9])-\d{3}-\d{4}$
is the regular expression that you need:
Starting with the beginning of the string (so no other digits sneak in)
09 is constant
1 can be combined with any subsequent digit, 3 cannot be combined with 0
-, and 3 digits
-, and 4 digits
End of string (again, so no other digits sneak in)
... but don't take my word for it, run it (I would recommend adding more test numbers):
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");
}
It's Simple. Iran TCI Phone in Any Format
Supports These Formats
+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);
}
Using this code in 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;
}
//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...
}
});
this is best answer, i tested all answers but this is easy and best
精彩评论