?
Solved

Replace Letters in String with Numbers

Posted on 2007-10-02
6
Medium Priority
?
1,400 Views
Last Modified: 2010-08-05
I need to have a simple JavaScript form on a page that a user can enter a MAC Adress and have it 'translated' to the numbers the user would push on their telephone keypad to enter the number.  Very similar to a 1-800 number when the telephone number spells out a word - 1-800-GET-BEER would be 1-800-438-2337.  In this case however, it is much simpler as there are far fewer letters.

If someone could give me the whole html page from <html> to </html> that would be awesome!

Here are the basic rules for this translation:

i.      0  9 = 0  9
ii.      A = 22
iii.      B = 222
iv.      C = 2222
v.      D = 33
vi.      E = 333
vii.      F = 3333

So if I typed into the form:  000b8200e395  I would get the number -->  0002228200333395

The 'B' was converted to '222' and the 'E' was converted to '333' and the string was put back together.


Thanks!!!
0
Comment
Question by:joedunn
  • 2
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 1000 total points
ID: 20000242
Here an example:


<script>

var num = {A:22,B:222,C:2222,D:33,E:333,F:3333};

alert(reNum("000b8200e395"));


function reNum(theStr){
  for(var r in num){
    theStr = theStr.replace(RegExp(r,"gi"),num[r]);
  }
  return theStr;
}

</script>



0
 
LVL 7

Assisted Solution

by:bdichiara
bdichiara earned 1000 total points
ID: 20000339
Give this a try:

function convertMacAddress(macAddress){
      var txt = macAddress.toLowerCase();

      newTxt = txt.replace('a','22');
      newTxt = newTxt.replace('b','222');
      newTxt = newTxt.replace('c','2222');
      newTxt = newTxt.replace('d','33');
      newTxt = newTxt.replace('e','333');
      newTxt = newTxt.replace('f','3333');

      return newTxt;
}

function convertIt(){
      var string = document.forms['mac_convert'].elements['macAddress'].value;
      mAddress = convertMacAddress(string);
      document.getElementById('result').innerHTML = mAddress;
      return false;
}

<form id="mac_convert" name="mac_convert" action="" method="get">
<input type="text" name="macAddress" id="macAddress" /><input type="submit" value="convert" onclick="return convertIt();" />
</form>
<div id="result"></div>
0
 
LVL 7

Expert Comment

by:bdichiara
ID: 20006444
Zvonko's answer is better. I was never really good at Regular expressions.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20008933
Thanks :)
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20432432
Forced accept.

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question