By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,034 Members | 960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,034 IT Pros & Developers. It's quick & easy.

How to convert the value of textbox to number

P: 75
I want to ask. how do i start codding this ?
i want to convert the value of textbox to number.

example the value of textbox is " box "

and there are the right value per letter.

b = 1
o = 3
x = 2
... and so on.

hmm, can i make it in java script or in php ?
Jul 15 '12 #1
Share this Question
Share on Google+
2 Replies


Claus Mygind
100+
P: 571
If you want the conversion to happen when the user is typing the content into the text "box" then you will have to use javaScript. If you want the conversion to happen when the user submits the form to the server you will have to use php

Here is a js script that changes the content with the onChange() event handler. You may be able to make it faster by using regExp. Or you could even have it convert as the user is typing each character by using the onkeypress() event handler.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.         <script type="text/javascript">
  5.             function convertTxtBox(obj){
  6.  
  7.                 //capture the id and value of the control element being processed
  8.                 var elm = obj.id;
  9.                 var val = obj.value;
  10.  
  11.                 //create an object with conversion values
  12.                 lookup = {b:1, o:3, x:2};
  13.  
  14.                 //initilize return value
  15.                 var retVal = '';
  16.  
  17.                 //loop through each letter in text box and convert
  18.                 for (var i = 0, l = val.length; i < l; i++) {
  19.  
  20.                     //get the current charcter in the string
  21.                     var re = val[i];
  22.  
  23.                     //replace numeric value found in lookup object or return same character if not found
  24.                     retVal += ( lookup[re] ) ?lookup[re] : re;
  25.                 }
  26.                 //replace the text box value with converted value
  27.                 document.getElementById(elm).value = retVal;
  28.             }
  29.         </script>
  30.     </head>
  31.     <body>
  32.         <input type="text" id="txtBox" onchange="convertTxtBox(this);"/>
  33.     </body>
  34. </html>
  35.  
Jul 15 '12 #2

P: 75
thank for help ...........
Jul 16 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.