• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 820
  • Last Modified:

Validate Phone Number Prefix within an Area Code

I am looking for a way to verify telephone number Prefix within the Area Code.   I am unable to find any  means to do this when the user is submitting their phone number into my website form.  I would like to validate the area code and prefix after they click Submit and  only let them go forward if they match.

Does anyone know of a script that can do this or where to find something that can process this?
0
ebizpros
Asked:
ebizpros
  • 4
  • 3
  • 2
  • +3
3 Solutions
 
jklNYCCommented:
are you asking about the validation logic or are you looking for the actual data with which to match the area code to the prefix?
0
 
ebizprosAuthor Commented:
I am asking if there is some type of script out there that can do this already rather than having to pay someone to build one.
0
 
siva_sivaCommented:
Use any of the below function code to validate the phone numbers

function isValid(pno) {
      //Phone Number (North America)  
      //Matches 3334445555, 333.444.5555, 333-444-5555, 333 444 5555, (333) 444 5555 and all combinations thereof.  
      //Replaces all those with (333) 444-5555  
      var regex = /\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/i;
      return regex.test(pno);
}

function isValid(pno) {
      //Phone Number (North America)  
      //Matches 3334445555, 333.444.5555, 333-444-5555, 333 444 5555, (333) 444 5555 and all combinations thereof.  
      var regex = /\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/i;
      return regex.test(pno);
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jklNYCCommented:
siva's solution is the right approach to use, if all you wanted to do was validate the phone number format.

i was under the impression you wanted to validate that the prefix was a valid "value" for the given area code. for something like that, it will be a more complicated process. i don't know of any scripts out there that do this.

i'm sure one could be built, but that would involve purchasing a database of valid phone numbers with which to validate against.
0
 
b0lsc0ttCommented:
You would have to pay, and continue to pay, for this type of validation.  The prefixes change often enough that this would be expensive.  If a valid cell number is a must then do you require it to be text enabled to.  It would be better to design a script to send a text message and wait for a reply as validation.  Another option is to use some manual method (i.e. call the phone) to validate the info and account.

There isn't a general rule for the prefixes so a simple validation with it will be impossible.  Let me know if you have a question about any of this.

bol

0
 
ebizprosAuthor Commented:
Yes, the data of the actual area codes and all prefixes within them would have to be purchased and thats not the problem  

 I just want to verify that when the person enters their phone number, the Prefix simply exists within the Area Code that was entered.  For example if the phone number is 815-398-0000, I would want to check first that the area code 815 exists and then check that 398 is in fact a valid Prefix within that Area Code.
0
 
jklNYCCommented:
Ok, so let's assume you've purchased the database with the necessary telephone matching information.

I don't think theres any good (or easy) way to do this with just javascript. You would need to incorporate some kind of dynamic server side scripting (e.g. asp, jsp or php) and a server-side web database (e.g. ms sql, oracle or mySql) setup. once you have this setup, doing the validation is actually quite simple, as it would require at most 2 queries of the database.
0
 
b0lsc0ttCommented:
I would strongly advise against Javascript.  The data (all of the zones and prefixes) would need to be loaded in the "html."  There is no advantage to it and it will bloat the pages severly.  I agree with jklNYC on using a server language and would even suggest you do just server validation (not try AJAX since I see no need).  A properly setup database could provide the result with 1 query I believe.

Let us know what database or server language you use and what information you need for this question.

bol
0
 
netmunkyCommented:
you can use ajax to check against a web service that provides an npa/nxx lookup such as http://www.quentinsagerconsulting.com/webservices.htm
which may be cheaper than purchasing the database, depending on how many lookups you need to do.

you can also find outdated npa/nxx databases for free
http://sd.wareonearth.com/~phil/npanxx/
or just the npa portion
http://www.nanpa.com/npa/AllNPAs_0906.zip

or purchase the full up to date database
http://www.area-codes.com/area-code-database.asp
0
 
b0lsc0ttCommented:
Unless you use a server script page to get the results you will most likely get a script error trying to access another domain.  Browsers prevent Internet pages from doing this for security reasons (cross domain scripting).  AJAX might still be a useful part of this process (I wouldn't bother though) but it should error if you try to use it to query the other site directly.

Let me know if you have a question.

bol
0
 
netmunkyCommented:
ajax would allow you to script getting data from another server client side (otherwise how would you explain embedding google maps in various websites?)

simple example:
<script>
var req = new XMLHttpRequest();
req.onreadystatechange = handler;
req.open( 'GET', "http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Everything&output=xml" );
req.send();

function handler()
{
  if(this.readyState == 4 && this.status == 200) { alert( this.responseXML.xml ); }
}
</script>

javascript does prevent you from accessing browser windows that are not in the same domain (cross site scripting)
0
 
b0lsc0ttCommented:
>> ajax would allow you to script getting data from another server client side <<

No, with the exception of certain (rare) cases.  You can't expect it to work for any URL you try and most won't.  Some browser versions (e.g. IE6) may allow it after a security "warning" if the user permits but this would be bad to depend on especially if you want your page to be cross browser compatible.  Try getting this question if you want to test it.  If you want a different page then try with http://rss.cnn.com/rss/cnn_topstories.rss.

Google's API does something different than just AJAX.  There server is setup for this, a user uses their script file as an external js file, and a registration is involved.  Other sites could offer something similar but the key is it isn't just Javascript and the xmlhttp object getting the response.  Some type of "proxy" script has to help and that has to be on the same domain as the main page (or appear to be, like an API).

I am not saying this validation can't be done.  I just wanted to make sure that it was understood that a server script will need to be involved, unless the external site provides some API, etc like Google.

Let me know if there is a question about this.

bol
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now