US Phone Number Verification

假如想象 提交于 2019-11-26 14:44:16

问题


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?


回答1:


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).




回答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}$', $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:

  • PHP at php.net: http://php.net/regex
  • C# at MSDN
  • Java: http://java.sun.com/developer/technicalArticles/releases/1.4regex/



回答3:


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.




回答4:


Amybe take a look at the answers to this question.




回答5:


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




回答6:


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.




回答7:


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).




回答8:


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]




回答9:


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.




回答10:


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




回答11:


In my research that I should have done beforehand >.< I found that 7 identical digits are valid phone numbers. So I can count out that rule.




回答12:


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;

    }



回答13:


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.




回答14:


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.



来源:https://stackoverflow.com/questions/175488/us-phone-number-verification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!