How to convert phone number LETTERS into DIGITS in JavaScript

0

How to translate letters used in phone numbers into digits, i.e. input string '1-800-FLOWERS' should return '1-800-3569377', using a Javascript function? Below is a matrix of digital keys with corresponding letters taken from Android ICS dialer keypad. Thanks!

enter image description here

Tags:

2 answers

Here is what you need in its most straightforward implementation. This function would convert Phone Number letters into digits

<script type="text/javascript" language="JavaScript">

function convertPhoneLetters(input) {

var inputlength = input.length;
input = input.toLowerCase();
var phonenumber = "";
for (i = 0; i < inputlength; i++) {
var character = input.charAt(i);


switch(character) {

case '0': phonenumber+="0";break;
case '1': phonenumber+="1";break;
case '2': phonenumber+="2";break;
case '3': phonenumber+="3";break;
case '4': phonenumber+="4";break;
case '5': phonenumber+="5";break;
case '6': phonenumber+="6";break;
case '7': phonenumber+="7";break;
case '8': phonenumber+="8";break;
case '9': phonenumber+="9";break;

case 'a': case 'b': case 'c': phonenumber+="2";break;
case 'd': case 'e': case 'f': phonenumber+="3";break;
case 'g': case 'h': case 'i': phonenumber+="4";break;
case 'j': case 'k': case 'l': phonenumber+="5";break;
case 'm': case 'n': case 'o': phonenumber+="6";break;
case 'p': case 'q': case 'r': case 's': phonenumber+="7";break;
case 't': case 'u': case 'v': phonenumber+="8";break;
case 'w': case 'x': case 'y': case 'z': phonenumber+="9";break;

default: phonenumber+=character;

   }
}

return phonenumber;
}

</script>

P.S. Here is a similar question and the answer on how to do the same in Microsoft SQL Server database using T-SQL function

0

Alphanumeric phone numbers is really a pain. I want to call a company and they have this alpha numeric phone number. I know the company name and their website but contacting via contact form didn't help. How do I convert or dial (213) 915-TEAS

0

Post Answer