Formatting textboxes

S

Sue

Hi,

Is there a way to format a textbox such that when the
user types in the number 4000 it will be automatically
formatted to $4,000?

Thanks.
 
K

Kevin Spencer

See my reply in the client newsgroup.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
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);">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top