[英]how to check that a number is a mobile number of a user in pakistan and not a landline number
What i have done is remove the +92 or 0092 from the start and used the following code to check if it is a valid mobile number for a pakistani person我所做的是从一开始就删除 +92 或 0092 并使用以下代码检查它是否是巴基斯坦人的有效手机号码
if(preg_match('/3[0-4][0-9](?:.*)/', $number) == 1) {
//Pakistani mobile number
} else {
// not a pakistani mobile number
}
is this approach right?这种方法对吗?
Based on the description found on wikipedia: Telephone_numbers_in_Pakistan , 根据Wikipedia上的描述:Telephone_numbers_in_Pakistan ,
then something like this pattern: 然后像这样的模式:
^0?3(?:[0-46]\d|55)\d{7}$
Or without considering the special case of operator 5 (SCO): 或不考虑运营商5(SCO)的特殊情况:
^0?3[0-6]\d{8}$
Or with including the country prefix as an optional group: 或包含国家/地区前缀作为可选组:
^((?:00|\+)92)?(0?3(?:[0-46]\d|55)\d{7})$
Php Test Snippet: Php测试摘要:
<?php
$telnumber='03441234567';
if(preg_match('/^((?:00|\+)92)?(0?3(?:[0-46]\d|55)\d{7})$/', $telnumber))
{
echo "matches pattern";
} else
{
echo "doesn't match pattern";
}
(^\+92[0-9]{10}$)|(^\0092[0-9]{10}$)
Here string append +92 initially then check next 10 digit or if there is 0092 in front of number then it consists of 10 digit number. 此处首先附加字符串+92,然后检查下一个10位数字,或者数字前面是否有0092,则该字符串由10位数字组成。 The +92 and 0092 check your ISD code initially not other. +92和0092首先检查您的ISD代码。
For only accept \d{4} and 9 digit ^((0)(3))([0-9]{9})$只接受 \d{4} 和 9 位数字 ^((0)(3))([0-9]{9})$
Pakistan mobile number examples:巴基斯坦手机号码示例:
+923331234567 +923331234567
00923331234567 00923331234567
03331234567 03331234567
try this regex:试试这个正则表达式:
"^((?:00|\\+)92)?(0?3(?:[0-4])\\d{7})\$"
((?:00|\\+)92)?
-> 0092 or +92 or none -> 0092 或 +92 或无
0?3
-> 03 or 3 0?3
-> 03 或 3
(?:[0-4])
-> after above string only 0,1,2,3,4 can be accepted (eg 031, 032, 033, 034) (?:[0-4])
-> 在上面的字符串之后只能接受 0、1、2、3、4(例如 031、032、033、034)
\\d{7}
-> length 7 characters will be accepted after above string \\d{7}
-> 在上述字符串之后将接受长度为 7 个字符
Note: other country regex can be made from these examples easily注意:其他国家的正则表达式可以很容易地从这些例子中制作出来
Use the following python code to validate Pakistani Phone Number.使用以下 python 代码验证巴基斯坦电话号码。 It checks country code (+92, 0092, 92, 0)
followed by a 3 and then for carrier code of 2 digits with limit of first digit (0 - 4)
and limit of second digit (0 - 9)
and at the end 7 digits in range (0 - 9)
.它检查国家代码(+92, 0092, 92, 0)
后跟 3,然后检查 2 位数字的承运人代码,第一位数字限制(0 - 4)
和第二位数字限制(0 - 9)
,最后范围(0 - 9)
中的 7 位数字。
import re
def validate_not_mobile(value):
pat = re.compile(r'(^((\+92)?(0092)?(92)?(0)?)(3)([0-4]{1})([0-9]{1})([0-9]{7})$)')
if re.fullmatch(pat, value):
print(f"'{value}' is a valid number!")
else:
print(f"'{value}' is NOT a valid number!")
I have tested the code with several test numbers and it has worked fine.我已经用几个测试编号测试了代码,并且运行良好。 If there is any further update I'll update it here.如果有任何进一步的更新,我会在这里更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.