adding of two digits in currency form

Hello everybody,<br />
<br />
Ive got a question.<br />
<br />
Ive got a form where the user can fill in a currency amount.<br />
<br />
I would like a javascript which automaticly adds two digits and a comma behind the number the user enters.<br />
<br />
For example:<br />
<br />
The user enters 50<br />
automaticly and immediate it changes to 50,00<br />
<br />
The user can't make use of a comma or a point.<br />
<br />
Can somebody help me with this???<br />
<br />
greetings Lennert<!--content-->Wait, what do you want? You realize that currency uses decimal points, no commas. 50 USD is also $50.00 not 50,00. If you'r talking thousands, then maybe. If the user wants 50 thousands, than the script makes it 50,000. But I'm confused as to what you're looking for.<!--content-->Actually, some countries use a comma instead of the period as a separator. You can test the different languages by making a change, and applying a VBScript to Internet Explorer (only IE, JS isn't cool enough to do this)<script type="text/javascript"><br />
<br />
var decChar = "." // make dec char variable so non-IE users get a default period<br />
<br />
</script><br />
<script type="text/VBScript"><br />
<br />
'Declare Variables<br />
Dim strx, GetDecimalChar 'vars for decimal character as set by settings<br />
<br />
' Get the decimal char<br />
strx = CStr(CDbl(1/2)) <br />
GetDecimalChar = Mid(strx, 2, 1) <br />
<br />
'make a javascript variable from the decimal character<br />
document.write("<script type='text/javascript'> decChar = '" & GetDecimalChar & "'; </sc"&"ript>")<br />
<br />
</script><br />
<script type="text/javascript"><br />
<br />
var numIn = "4.03" // money amount<br />
var numOut = numIn.replace('.',decChar) // change period to correct char<br />
alert(numOut) // alert output<br />
<br />
</script>It's a simple script, and I commented it for ease of use.<br />
<br />
What you are looking to do should not be done. If they enter 50, and you add on ",00", what if they wnated to enter 500? You could using the onblur event call a function which tests for a comma, and if its not there, add ",00" to the end. Regular expression test would do:if(/\d+,\d{2}\/.test(document.forms['form_name']['money'].value)) {<br />
// money good<br />
}<br />
else {<br />
// money not good, add on the ",00"<br />
}That would be a function you'd call on the onBlur event of the text box :)<br />
<br />
Dr. Script<!--content-->Yes it is confusing indeed.<br />
Thats the problem we have. Some people use comma's and some use points. Some think it is thousands and some think it is just 50 euro.<br />
<br />
What I want is that the users (which are all Dutch) is typing and automaticly a comma and two digits is added.<br />
<br />
So if he types 50 it immidiately becomes 50,00<br />
if he types 500 it immediately becomes 500,00<br />
if he types 5000 it immediately becomes 5000,00<br />
if he types 50.000 it immediately becomes 50000,00<br />
<br />
I hope it is clear to you.. (It's not even clear to me :-) but anyway) <br />
<br />
greetings, Lennert<!--content-->We get this request from time to time and I'm rather fond of the solution I worked up a few years ago. See <!-- m --><a class="postlink" href="http://www.webdeveloper.com/forum/showthread.php?s=&threadid=19087">http://www.webdeveloper.com/forum/showt ... adid=19087</a><!-- m -->. posted by Zwelgje <br />
So if he types 50 it immidiately becomes 50,00<br />
if he types 500 it immediately becomes 500,00<br />
if he types 5000 it immediately becomes 5000,00<br />
if he types 50.000 it immediately becomes 50000,00<br />
<br />
<br />
To type 500, you need to type 50 first, so before you got the chance to type 500 it automatically changes to 50,00 .<br />
<br />
Might get a bit annoying...<br />
<br />
Neil<!--content-->
 
Back
Top