美国电话号码验证

发布于 2024-07-06 09:49:11 字数 509 浏览 13 评论 0原文

我有一个网站表单,需要输入美国电话号码以供后续使用,在这种情况下这是非常有必要的。 我想尝试消除用户输入垃圾数据 330-000-0000。 我已经看到了第三方为您验证电话号码的一些选项,但是我不知道这是否是这种情况的最佳选择。 但是,如果您使用过这些第三方之一并且可以提出建议,我们也将不胜感激。

不过,我正在考虑根据一组规则检查该号码,以尝试缩小收到的垃圾电话号码的范围。

  • 不是 555 号码
  • 不包含 7 个相同数字的
  • 有效区号(这很容易获得)
  • 不是 123-1234 或 123-4567
  • 我想我也可以算出 867-5309 (heh*)

这是否会导致您能想到的不允许用户输入电话号码的情况? 您能想到电话号码不应包含的其他规则吗? 还有其他想法吗?

I have a website form that requires a US phone number input for follow up purposes, and this is very necessary in this case. I want try to eliminate users entering junk data 330-000-0000. I have seen some options of third parties that validate phone numbers for you, however idk if that is the best option for this situation. However if you have every used one of these third parties and can make a recommendation that would also be greatly appreciated here.

However I am considering checking the number against a set of rules to just try to narrow down the junk phone numbers received.

  • not a 555 number
  • does not contain 7 identical digits
  • valid area code (this is readily available)
  • not a 123-1234 or 123-4567
  • I guess I could also count out 867-5309 (heh*)

Would this result in any situations that you can think of that would not allow a user to enter their phone number? Could you think of any other rules that a phone number should not contain? Any other thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

寄意 2024-07-13 09:49:11

在我看来,你在这方面付出的努力超出了应有的程度。 考虑一下:

如果您的目的是防止输入错误的电话号码,那么您只需进行一次非常简单的检查,就可以查出 90% 以上的错误。

如果您的目的是试图强迫用户提供有效的数字,无论他们是否愿意提供该信息,那么您就承担了一项无望的任务 - 即使您能够访问 100% 准确、最新的信息第二个电信公司数据库来验证输入的确切号码当前是否有效,但您仍然无法保证他们给您的号码是他们自己的。 再说一次,简单的检查将挫败大多数输入虚假号码的人,但那些愿意尝试超过两三次的人将找到一种方法来挫败您获取其号码的尝试。

无论哪种方式,一个简单的测试都会给你带来好的结果,而进入更复杂的规则集将花费越来越多的时间,同时给你带来的好处越来越少(同时还可能增加误报,正如“七个规则”所示)相同数字”和 867-5309 例)。

It seems to me that you're putting more effort into this than it warrants. Consider:

If your purpose is to guard against mis-entered phone numbers, then you can probably catch well over 90% of them with just a very simple check.

If your purpose is to try to force users to provide a valid number whether they want to give that information out or not, then you've taken on a hopeless task - even if you were able to access 100% accurate, up-to-the-second telco databases to verify that the exact number entered is currently live, you still don't gain any assurance that the number they gave you is their own. Once again, a simple check will foil the majority of people entering bogus numbers, but those who are willing to try more than two or three times will find a way to defeat your attempts to gain their numbers.

Either way, a simple test is going to get you good results and going into more complex rule sets will take up increasingly more time while providing increasingly little benefit to you (while also potentially adding false positives, as already shown with the "seven of the same digit" and 867-5309 cases).

久光 2024-07-13 09:49:11

您可以使用正则表达式在应用程序内部进行电话号码验证。 根据您的语言,您可以调用一个函数,如果提供的电话号码与表达式匹配,该函数将返回 true。

在 PHP 中:

function phone_number_is_valid($phone) {
    return (eregi('^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}

您可以在线查找不同的正则表达式。 我在 http://regexlib.com/DisplayPatterns.aspx 找到了上面的一个?categoryId=7&cattabindex=2

编辑:正则表达式的一些特定于语言的站点:

, $phone)); }

您可以在线查找不同的正则表达式。 我在 http://regexlib.com/DisplayPatterns.aspx 找到了上面的一个?categoryId=7&cattabindex=2

编辑:正则表达式的一些特定于语言的站点:

You can do phone number validation internally in your app using regular expressions. Depending on your language you can call a function that will return true if a supplied phone number matches the expression.

In PHP:

function phone_number_is_valid($phone) {
    return (eregi('^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}

You can look up different regular expressions online. I found the one above one at http://regexlib.com/DisplayPatterns.aspx?categoryId=7&cattabindex=2

Edit: Some language specific sites for regular expressions:

, $phone)); }

You can look up different regular expressions online. I found the one above one at http://regexlib.com/DisplayPatterns.aspx?categoryId=7&cattabindex=2

Edit: Some language specific sites for regular expressions:

享受孤独 2024-07-13 09:49:11

867-5309 是分配给不同区号的人员的有效电话号码。

867-5309 is a valid phone number that is assigned to people in different area codes.

夜声 2024-07-13 09:49:11

如果您可以验证区号,那么除非您真的非常需要知道他们的电话号码,否则您可能会尽可能合理地进行操作。

If you can verify the area code then unless you really, really need to know their phone number you're probably doing as much as is reasonable.

灯角 2024-07-13 09:49:11

在 Django 中,有一个名为 localflavor 的漂亮小 contrib 包,其中包含许多特定于国家/地区的验证代码,例如邮政编码或电话号码。 您也可以查看源代码,了解 django 如何针对您想要使用的国家/地区处理这些内容; 例如: 美国表单验证。 这对于您了解甚少的国家来说是一个很好的信息来源。

In Django there is a nice little contrib package called localflavor wich has a lot of country specific validation code, for example postal codes or phone numbers. You can look in the source too see how django handles these for the country you would like to use; For example: US Form validation. This can be a great recourse for information about countries you know little of as well.

等风也等你 2024-07-13 09:49:11

您的客户仍然可以执行我所做的操作,即给出本地 moviefone 号码。

此外,123-1234 或 123-4567 只是无效数字,因为前缀以 1 开头,但 234-5678 或 234-1234 实际上是有效的(尽管看起来是假的)。

Your customers can still do what I do, which is give out the local moviefone number.

Also, 123-1234 or 123-4567 are only invalid numbers because the prefix begins with a 1, but 234-5678 or 234-1234 would actually be valid (though it looks fake).

夏末染殇 2024-07-13 09:49:11

也许看看这个问题的答案。

Maybe take a look at the answers to this question.

内心荒芜 2024-07-13 09:49:11

如果您只使用美国和加拿大格式的数字,我认为以下正则表达式可能有效:
[2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0- 9]& ![2-9][0-9][0-9]-555-[0-9][0-9][0-9][0-9]

If you're sticking with just US- and Canada-format numbers, I think the following regex might work:
[2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9] & ![2-9][0-9][0-9]-555-[0-9][0-9][0-9][0-9]

西瑶 2024-07-13 09:49:11

您还需要考虑现在在某些地区使用的十位数拨号:这与长途拨号(即 303-555-1234,而不是 1-303-555-1234)不同。 在某些地方,有效的电话号码长度为十位数字; 在其他国家,这个数字是七。

You also need to take into account ten-digit dialing, which is used in some areas now: this is different from long-distance dialing (ie, 303-555-1234, as opposed to 1-303-555-1234). In some places, a valid phone number is ten digits long; in others, it is seven.

久夏青 2024-07-13 09:49:11

这是我使用的一个快速功能(如下)。 我确实可以访问包含区域代码和前缀数据的邮政编码数据库,该数据库每月更新一次。 我经常考虑进行数据提取来确认区号的前缀是否存在。

    public static bool isPhone(string phoneNum)
    {
        Regex rxPhone1, rxPhone2;

        rxPhone1 = new Regex(@"^\d{10,}$");
        rxPhone2 = new Regex(@"(\d)\1\1\1\1\1\1\1\1\1");

        if(phoneNum.Trim() == string.Empty)
            return false;

        if(phoneNum.Length != 10)
            return false;

        //Check to make sure the phone number has at least 10 digits
        if (!rxPhone1.IsMatch(phoneNum))
            return false;

        //Check for repeating characters (ex. 9999999999)
        if (rxPhone2.IsMatch(phoneNum))
            return false;

        //Make sure first digit is not 1 or zero
        if(phoneNum.Substring(0,1) == "1" || phoneNum.Substring(0,1) == "0")
            return false;

        return true;

    }

This is a quick function that I use (below). I do have access to a zipcode database that contains areacode and prefix data which is updated monthly. I have often thought about doing a data dip to confirm that the prefix exists for the area code.

    public static bool isPhone(string phoneNum)
    {
        Regex rxPhone1, rxPhone2;

        rxPhone1 = new Regex(@"^\d{10,}$");
        rxPhone2 = new Regex(@"(\d)\1\1\1\1\1\1\1\1\1");

        if(phoneNum.Trim() == string.Empty)
            return false;

        if(phoneNum.Length != 10)
            return false;

        //Check to make sure the phone number has at least 10 digits
        if (!rxPhone1.IsMatch(phoneNum))
            return false;

        //Check for repeating characters (ex. 9999999999)
        if (rxPhone2.IsMatch(phoneNum))
            return false;

        //Make sure first digit is not 1 or zero
        if(phoneNum.Substring(0,1) == "1" || phoneNum.Substring(0,1) == "0")
            return false;

        return true;

    }
我最亲爱的 2024-07-13 09:49:11

我不知道这是不是正确的地方,它是一个格式化函数而不是验证函数,我想让我们与社区分享它,也许有一天它会有所帮助..

Private Sub OnNumberChanged()
    Dim sep = "-"
    Dim num As String = Number.ToCharArray.Where(Function(c) Char.IsDigit(c)) _
                                                 .ToArray
    Dim ext As String = Nothing
    If num.Length > 10 Then ext = num.Substring(10)
    ext = If(IsNullOrEmpty(ext), "", " x" & ext)
    _Number = Left(num, 3) & sep & Mid(num, 4, 3) & sep & Mid(num, 7, 4) & ext
End Sub

我的验证函数是这样的:

Public Shared Function ValidatePhoneNumber(ByVal number As String)
    Return number IsNot Nothing AndAlso number.ToCharArray. _
                                  Where(Function(c) Char.IsNumber(c)).Count >= 10
End Function

我称之为最后一个函数@实体的OnNumberChanging(number As String)方法。

I don't nkow if this is the right place, it's a formatting function rather than a validation function, I thought let's share it with the community, maybe one day it will be helpful..

Private Sub OnNumberChanged()
    Dim sep = "-"
    Dim num As String = Number.ToCharArray.Where(Function(c) Char.IsDigit(c)) _
                                                 .ToArray
    Dim ext As String = Nothing
    If num.Length > 10 Then ext = num.Substring(10)
    ext = If(IsNullOrEmpty(ext), "", " x" & ext)
    _Number = Left(num, 3) & sep & Mid(num, 4, 3) & sep & Mid(num, 7, 4) & ext
End Sub

My validation function is like so:

Public Shared Function ValidatePhoneNumber(ByVal number As String)
    Return number IsNot Nothing AndAlso number.ToCharArray. _
                                  Where(Function(c) Char.IsNumber(c)).Count >= 10
End Function

I call this last function @ the OnNumberChanging(number As String) method of the entity.

小红帽 2024-07-13 09:49:11

对于美国和国际电话验证,我发现此代码最合适:

((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$

您可以找到一个(尽管有点过时)讨论 这里

For US and International Phone validation I found this code the most suitable:

((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$

You can find an (albeit somewhat dated) discussion here.

血之狂魔 2024-07-13 09:49:11

这些参数对我来说看起来相当不错,为了安全起见,我也可能会避免使用以 911 开头的数字。

Those parameters look pretty good to me, I might also avoid numbers starting with 911 just to be safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文