US Phone Number Verification

前端 未结 13 1662
眼角桃花
眼角桃花 2020-11-30 14:00

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

相关标签:
13条回答
  • 2020-11-30 14:32

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 14:39

    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.

    0 讨论(0)
  • 2020-11-30 14:45

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

    0 讨论(0)
  • 2020-11-30 14:45

    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;
    
        }
    
    0 讨论(0)
  • 2020-11-30 14:48

    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/
    0 讨论(0)
提交回复
热议问题