Friday, 15 August 2014

How to convert letters to numbers in javascript? -



How to convert letters to numbers in javascript? -

i have been trying create simple javascript programme user types in words/sentences, , programme shows numbers come in text in flip phone. instance, if entered "add" output "122".

how this? have tried lot of different things, , nil works. seems should simple program. can either help me this, or link me similar program?

thanks.

function str2num(mystr) { mystr = mystr.touppercase(); var conv = []; var l = mystr.length; (var i=0; conv[i] = mystr.charcodeat(i)-65; } homecoming conv; }

read this

http://www.yaldex.com/fsequivalents/phonenumberconverter.htm phone converter

in case link breaks here's code:

<script language="javascript" type="text/javascript"> <!-- begin function convert(input) { var inputlength = input.length; input = input.tolowercase(); var phonenumber = ""; (i = 0; < 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 '-': phonenumber+="-";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; } } document.myform.number.value = phonenumber; homecoming true; } // end --> </script> <form name=myform> <table border=0> <tr> <td>alphanumeric #:</td> <td><input type=text size=20 maxlength=20 name=alphabet value="rachel"></td> </tr> <tr> <td>converted #:</td> <td><input type="text" size=20 maxlength=20 name="number"></td> </tr> <tr> <td align=center colspan=2><input type=button value="convert" onclick="return convert(document.myform.alphabet.value)"></td> </table> </form>

javascript

No comments:

Post a Comment