douluan1533 2014-09-26 20:50
浏览 51
已采纳

仅验证美国电话号码

I am presently building a website, in PHP, that users will be able to interact with via SMS messaging. Consequently, I need to store their phone numbers in my database. Since I am only allowing SMS interactions in the U.S. right now, I need to check that the submitted phone numbers conform to the U.S. format. Generally, this means a few things: 1 must be the first number, the area code must not begin with 0 or 1, and the string without the leading 1 must be 10 digits (3 digit area code plus 7 digit number).

Despite all of these requirements, I want users to feel free to enter their number in whatever format they prefer without causing an error.

  • 写回答

2条回答 默认 最新

  • dtjw6660 2014-09-26 21:16
    关注

    Ignoring non-digits is the first step. The leading 1 is not required by all telephone companies, particularly near New Jersey. Dialing it there causes an error or a wrong number.

    The area code cannot be [2-9]11 nor [2-9]9[0-9]. Area codes with a 9 as the center digit are reserved for an as-yet-undecided scheme to address area code exhaustion.

    Exchanges also cannot begin with a 0 or 1, nor can they be [2-9]11.


    These restrictions are expressed with this code:

    $mobile = preg_replace ('/\D/', '', $trimmed['mobile']);
    
    if ($mobile[0] == '1') $mobile = substr ($mobile, 1);  // remove prefix
    
    $invalid = strlen ($mobile) != 10  ||
               preg_match ('/^1/',      $mobile) ||  // ac start with 1
               preg_match ('/^.11/',    $mobile) ||  // telco services
               preg_match ('/^...1/',   $mobile) ||  // exchange start with 1
               preg_match ('/^....11/', $mobile) ||  // exchange services
               preg_match ('/^.9/',     $mobile);    // ac center digit 9
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab