如何识别巴基斯坦手机号码而非固定电话?现有正则校验方案是否正确?
Is this Pakistani mobile number validation regex correct?
First off, let's be clear: the regex you're using isn't correct for validating Pakistani mobile numbers, and here's why:
Issues with your current regex
- Too permissive: The pattern
/3[0-4][0-9](?:.*)/will match any string that starts with300to349—even if it has non-numeric characters (like300-abc123) or is way too short/long (like300or3001234567890). Pakistani mobile numbers (after removing+92or0092) should be pure digits with a fixed length. - Incomplete prefix coverage: Pakistani mobile carriers use more prefixes than just
30to34. Prefixes like35(SCO),36,37,38, and39are all valid for mobile numbers, but your regex will flag these as non-mobile numbers incorrectly.
Correct validation approach
Pakistani mobile numbers, once stripped of the country code (+92 or 0092), follow this structure:
- Exactly 10 digits long
- Starts with
3(fixed for all mobile numbers; fixed lines use other starting digits like2,4,5, etc.)
Step 1: Preprocess the number (optional but recommended)
First, clean the input to remove any non-numeric characters (like spaces, hyphens, or parentheses) that users might enter:
$cleanNumber = preg_replace('/\D/', '', $number);
Step 2: Validate with a strict regex
Use this regex to check for a valid 10-digit number starting with 3:
if(preg_match('/^3[0-9]{9}$/', $cleanNumber) === 1) { // Valid Pakistani mobile number } else { // Not a valid Pakistani mobile number }
This ensures:
- The number is pure digits
- It's exactly the right length for a Pakistani mobile number (after country code removal)
- It covers all valid mobile prefixes used in Pakistan
内容的提问来源于stack exchange,提问作者Abeer Saeed
相关产品推荐
相关产品推荐

