Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate Text-Box,which accepts only figures/numbers with an M or T

Status
Not open for further replies.

sammes

Programmer
Jul 13, 2004
7
GB
Hi Programmer´s !

I have a problem. I just want to make a script, which creates a simple html input field which accepts only figures/numbers with the "extension" "M" (for million) or "T" (for thousand).

The next step should be, that the script convert this input value to the real figuer.

Example:

inputted value = 100M
output (e.g. to java) should be = 100000000 (100 million)
OR
inputted value = 100T
output = 100000 (100 thousand)

The script should generate the text-box and have this functionality.

I don´t know how to recognize the "M" or the "T" and convert themn with the entered number to the right value.

I would be great, if some has an idea !

regards

Michael
 

This should do the job you're after:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function convertNum() {
			var inputVal = document.forms[0].myValue.value;
			if (inputVal.length == 0) {
				alert('You didn\'t input anything!');
				return;
			}
			var lastChar = inputVal.charAt(inputVal.length-1).toUpperCase();
			if (lastChar != 'T' && lastChar != 'M') {
				alert('You didn\'t input anything ending with either \'T\' or \'M\'!');
				return;
			}

			var stringWithoutChar = inputVal.substr(0, inputVal.length-1);
			if (isNaN(stringWithoutChar)) {
				alert('You didn\'t enter a valid number!');
				return;
			}

			if (lastChar == 'T') {
				var newNum = parseInt(inputVal, 10) * 1000;
			} else if (lastChar == 'M') {
				var newNum = parseInt(inputVal, 10) * 1000000;
			} else {
				alert('You should never get this alert ;o)');
				return;
			}

			document.getElementById('myOutput').innerText = newNum;
		}
	//-->
	</script>
</head>
<body>
	<form>
		Input number: <input type="text" name="myValue"><br />
		Click <a href="javascript:convertNum();">here</a> to convert number.<br />
		Yourt converted number is: <span id="myOutput"></span>
</body>
</html>

Hope this helps,
Dan
 

Incidentally, if you don't want the user to be able to enter "t" or "m" (lowercase) as well as "T" or "M" (uppercase), then remove this:

Code:
.toUpperCase()

Dan
 
As an alternative, you might consider only allowing numbers in the field, then put a drop-down next to it with options M and T in it, setting the values to 1,000,000 and 1,000 respectively. In the end, you can multiply the value of the textbox by the value of the selectedIndex in the drop-down.

--Dave
 
Thanks !!
This works fine !
The script should not convert 100M to 100000000.
This wil be made in JAVA aftewards.

But now the requirements ahve changed.
I will post a new thread.

regards
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top