Link to home
Start Free TrialLog in
Avatar of ebizpros
ebizpros

asked on

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?
Avatar of jklNYC
jklNYC

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?
Avatar of ebizpros

ASKER

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.
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);
}
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.
Avatar of b0lsc0tt
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

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.
ASKER CERTIFIED SOLUTION
Avatar of jklNYC
jklNYC

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

Computer101
EE Admin