Link to home
Start Free TrialLog in
Avatar of iambagels
iambagelsFlag for United States of America

asked on

Validating phone and zip beyond a regular expression

I need a more bullet proof solution to validating phone numbers and postal codes (United States) I'm already using regular expressions that check for the right number of characters, no 555 numbers, no area codes starting with 0 or 1, and no area codes with 9 as a second digit. Zip codes I'm just doing a basic check for a 12345 or 12345-1234 zip code.

This isn't enough.

I was wondering if there is any public database I can connect to for validating phone numbers, zip codes, and states against each other to ensure that they are valid.
SOLUTION
Avatar of Scott Bennett
Scott Bennett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of divyeshhdoshi
divyeshhdoshi

Hi,

please specify this in regular expression

\[2-9]{1}\[0-8]{1}\[0-9]*

With Regards,
DIvyesh Doshi
iambagels> This isn't enough.

Why not?  What do you want the rules to be?

iambagels> I was wondering if there is any public database I can connect to for validating phone numbers, zip codes, and states against each other to ensure that they are valid.

Valid meaning that they exist too?  What if they have a newly issued zip code or telephone number?  What kind of business model do you have that would only accept business from a potentially stale validation cache?  And a public database is likely to always be stale.

What is your business reason which makes it so critical for the user to enter an existing telephone number?  I'm just curious as to whether a proper cost/benefit analysis had been done, that's all.  Many businesses tend to not care so much for invalid telephone numbers, and post codes lookups are commonly available (although in the UK, one would only really consider subscribing to the Royal Mail's PAF service).

I don't know about the US, but in the UK, you can SMS any telephone (if it's a non-SMS telephone they get a phone call from an automated Tom Baker http://news.bbc.co.uk/1/hi/entertainment/4665254.stm).  So you could send an activation code to the user's entered telephone number for them to use on your site.  I think this sounds very similar to stormists's suggestion, but may be cheaper.

--
Lee
As I said before validating the phone number area code to the city,state,zip in the address is a bad idea. I do agree with stormists idea that If you absolutely need to make sure their phone number is valid you should use a phone number validation service such as phoneconfirm.com. This is how many SSL Certificate authorities work when you purchase an ssl certificate.
Avatar of iambagels

ASKER

the reason i'm asking is because the company i work for does cost per lead (CPL) advertising. recently there have been problems with clients not accepting leads because the phone number was invalid. (they wont pay for invalid leads) i dont deal with the clients so i dont know exactly what this means. It could be a wrong number, It could be a n invalid number, i dont know for sure.

divyeshhdoshi: currently i'm using these three regex's to validate (this is ASP, i'm doing JavaScript checks too):
            if ( Len(home_phone) = 0 ) then
                  form_errors(err_num) = "'Home Phone' is invalid"
                  err_num = err_num + 1
            elseif ( ereg(home_phone,"^[2-9][0-8][0-9][- .]?[0-9]{3}[-. ]?[0-9]{4}$",true) = false ) then
            ' if the phone number does not meet the following rules:
            ' - must be 10 digits
            ' - area code cannot start with 0 or 1
            ' - 9 cannot be the second number of an area code
            ' - separators may be "-", " ", or "." and are optional
            ' - if separators are used, numbers must be grouped ### ### ####
                  form_errors(err_num) = "'Home Phone' is invalid"
                  err_num = err_num + 1
            elseif ( ereg(home_phone,"^[2-9][0-8][0-9][- .]?555[-. ]?[0-9]{4}$",true) = true ) then
            '' if the 4th, 5th and 6th digits are all "5" (### 555 ####)
                  form_errors(err_num) = "'Home Phone' is invalid"
                  err_num = err_num + 1
            elseif ( ereg(home_phone,"^[2-9]11[- .]?[0-9]{3}[-. ]?[0-9]{4}$",true) = true ) then
            '' if area code is #11
                  form_errors(err_num) = "'Home Phone' is invalid"
                  err_num = err_num + 1
            end if
you can validate it all you want, you can even match the area code to the zip, you can still be fake/wrong numbered. The only way to ensure the phone number is really the phone number of the person that is filling out the form is to have your system validate it, by calling the number while the person is filling out the form on your site, then have them enter a number/passphrase/code that is displayed on the site and confirm this is their phone number. this is the kind of service that comanies like phoneconfirm.com provide.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help guys. It looks like my company is looking into something like PhoneConfirm. Personally I've been saying what Lee posted. I don't think it's worth it.

Thanks