You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何识别巴基斯坦手机号码而非固定电话?现有正则校验方案是否正确?

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 with 300 to 349—even if it has non-numeric characters (like 300-abc123) or is way too short/long (like 300 or 3001234567890). Pakistani mobile numbers (after removing +92 or 0092) should be pure digits with a fixed length.
  • Incomplete prefix coverage: Pakistani mobile carriers use more prefixes than just 30 to 34. Prefixes like 35 (SCO), 36, 37, 38, and 39 are 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 like 2, 4, 5, etc.)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:52:41