views:

1533

answers:

13

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  A: 

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

Neall
I think that was a joke, dude...
DannySmurf
It's also illegal to sell your number if you don't like it (or buy it from someone else) without intervention from the phone company.There was a guy in NYC last year who was fined for doing it when he tried to sell 867-5309 :)
warren
And this would explain the joke for our non-US readers and those who cannot remember the 1980's. http://en.wikipedia.org/wiki/Jenny_(867-5309)
Onorio Catenacci
Sorry--the link's not parsing right for some reason. Anyway, look it up at Wikipedia if you don't get the joke. :-)
Onorio Catenacci
it links correctly now.. i fix0red wikipedia
Kip
A: 

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]

warren
anything that allows a number starting with 911 (or 411 for that matter) is probably not that great
tloach
I believe there are other *11 numbers that do things. See http://en.wikipedia.org/wiki/N11_code for more information, but I think it depends on your area. 911 and 411 are just about all over the US, though.
Thomas Owens
A: 

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.

DannySmurf
only for local calling, and even where it's 7 I believe you can use the full 10 digits
tloach
Not everywhere. In Alberta (where I live), before the switch to 10-digit dialing, dialing all ten resulted in a recording telling you to dial "1" first for long distance and then a disconnection.
DannySmurf
+2  A: 

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.

tloach
obviously, you would also want to verify 10 digits, I assumed that went without saying.
tloach
+3  A: 

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:

GloryFish
+1. Thanks for the link to C# reg expressions for phone numbers. Worked perfectly for me.
p.campbell
A: 

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

chills42
A: 

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.

Patcouch22
huh, didn't know those were allowed... nice find
chills42
+2  A: 

Amybe take a look at the answers to this question.

Rob Wells
Excellent! didn't come up in my related question.
Patcouch22
+1  A: 

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

Kip
No, the first of the final 7 digits can't be 1
Chris Marasti-Georg
Oh thanks I'll update it
Kip
+1  A: 

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.

D4V360
Nice code reuse. ;)
qstarin
+7  A: 

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

Dave Sherohman
A: 

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;

    }
J.Hendrix
A: 

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.

Shimmy