US Phone Number Verification

人盡茶涼 提交于 2019-11-27 09:30:53

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

GloryFish

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:

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.

Rob Wells

Amybe take a look at the answers to this question.

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

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.

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

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]

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.

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

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.

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;

    }

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.

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.

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