Formatting

S

Sue

Hi,

(I posted this on the programming newsgroup by accident.
Sorry. I was only told afterwards that this is a client
side issue)

Is there a way to format a textbox, or drop-downs such
that when the
user types in the number 4000 it will be automatically
formatted to $4,000 or the data will be pre-formatted in
the drop-down?

Thanks.
 
K

Kevin Spencer

Hi Sue,

I'm not sure who told you that you posted to the wrong group, but you
didn't. In any case, I'll help you now!

You would have to do this with client-side JavaScript in your page. It will
be a bit tricky, however. It's a simple enough matter to change the value of
a text box, but the real question is WHEN? You can't change it while the
user is typing ("onchange" event handler). You can change it when the text
box loses focus ("onblur"), which means that the user has clicked or tabbed
outside the text box. However, if the user submits the form while the text
box is in focus, the value will change, but the user will not see it. Of
course, that's a minor point. You could do it in the "onsubmit" event
handler for the form, but again, the user will never see it, while, if they
use the "onblur" of the text box, they might, as long as they haven't
submitted the form. So, I'll go for "onblur".

First, you have to create a function that changes the value. Example:

<script type="text/javascript"><!--
function updateBox(box)
{
box.value = "$" + box.value;
}
// --></script>

<input type="text" name="T1" size="20" onblur="updateBox(this)">

Of course, it gets a bit more complicated than that. For example, if you
want to insert commas, you have to do some rather tricky work. First, you
have to make sure that the value typed in is a number. Then you have to
count the digits, and concatenate strings together to form the new formatted
number.

If you really want something decent, I would Google JavaScripts for
formatting numbers. But that is basically the way it works.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
C

chris leeds

this is usually accomplished after the form data is submitted via some
server side scripting.
You may be able to find some specific JavaScript's that will do this before
the form is submitted. I've had good luck looking at www.javascript-2.com

HTH

--
Chris Leeds,
Microsoft MVP FrontPage

The email address on this posting is a "black hole". I got tired of all the
spam.
Please feel free to contact me here:
http://nedp.net/contact/
 
B

Bob Lehmann

It was posted in asp.general.

They directed her to a client-side group.

Bob Lehmann
 
M

MD Websunlimited

Hi Sue,

You'll need to utilize JavaScript or server side script to accomplish plus a event to cause the conversion.

Insert the following script in the head

<SCRIPT LANGUAGE="JavaScript"><!--
function outputMoney(number) {
return outputDollars(Math.floor(number-0) + '') + outputCents(number - 0);
}

function outputDollars(number) {
if (number.length <= 3)
return (number == '' ? '0' : number);
else {
var mod = number.length%3;
var output = (mod == 0 ? '' : (number.substring(0,mod)));
for (i=0 ; i < Math.floor(number.length/3) ; i++) {
if ((mod ==0) && (i ==0))
output+= number.substring(mod+3*i,mod+3*i+3);
else
output+= ',' + number.substring(mod+3*i,mod+3*i+3);
}
return (output);
}
}

function outputCents(amount) {
amount = Math.round( ( (amount) - Math.floor(amount) ) *100);
return (amount < 10 ? '.0' + amount : '.' + amount);
}


Then for the form text field that you wish to format use:

<input type="text" onblur="this.value = '$' + outputMoney(this.value);">
 
Top