How to convert the value of textbox to number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lyodmichael
    New Member
    • Jul 2012
    • 75

    How to convert the value of textbox to number

    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 ?
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    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.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<script type="text/javascript">
    			function convertTxtBox(obj){
    
    				//capture the id and value of the control element being processed
    				var elm = obj.id;
    				var val = obj.value;
    
    				//create an object with conversion values
    				lookup = {b:1, o:3, x:2};
    
    				//initilize return value
    				var retVal = '';
    
    				//loop through each letter in text box and convert
    				for (var i = 0, l = val.length; i < l; i++) {
    
    					//get the current charcter in the string
    					var re = val[i];
    
    					//replace numeric value found in lookup object or return same character if not found
    					retVal += ( lookup[re] ) ?lookup[re] : re;
    				}
    				//replace the text box value with converted value
    				document.getElementById(elm).value = retVal;
    			}
    		</script>
    	</head>
    	<body>
    		<input type="text" id="txtBox" onchange="convertTxtBox(this);"/>
    	</body>
    </html>

    Comment

    • lyodmichael
      New Member
      • Jul 2012
      • 75

      #3
      thank for help ...........

      Comment

      Working...