Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Area Code Lookup

    I am looking for a simple script or I hope it seems. I need the script to function like this:

    [text box] insert telephone number here in format like this xxx-xxx-xxxx

    but once the number is entered I would like for it to auto detect the area code and in a box or just as text to the right or left I would like for it to display the state that the area code corresponds with. I am using a set of predetermined area codes that I can enter myself once I am made aware of the script.

    IF this is possible then I would greatly appreciate the help!

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    Code:
    <script type="text/javascript">
    region=[//2 dimensional array containing area codes and region pairs
    ["201","nj"],
    ["202","dc"],
    ]
    
    
    function getRegion(code){
    	index=0;
    	while(true){
    		console.log(index);
    		if(region[index][0]==code){
    			return(region[index][1]);
    		}
    		index++;
    		if(index>=region.length){
    			return(undefined);
    		}
    	}
    }
    
    function getLocation(number){
    	out=document.getElementById('output');
    	var answer;
    	if(number.length<3){
    		return(void(0));
    	}
    	else{
    		var code=number.substring(0,3);
    		answer=getRegion(code)
    		if(answer==undefined){
    			out.innerText="unknown area code";
    		}
    		else{
    			out.innerText=answer;
    		}
    	}
    }
    </script>
    ...
    <form>
    	<input type="text" id="number" onkeyup="getLocation(this.value)">
    </form>
    <div id="output">
    </div>
    you might want to use excel to help make the array

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    what about an auto format?

    say for instance I plugin 2029878989 and want it to look like 202-987-8989

    is that possible?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •