By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

Auto Currency problem that I can' figure out...

P: 21

I have a script to format a text filed for currency. In general it works fine with 2 exceptions.

1) If you are in the field and enter the wrong amount when you back space the float does not change...
you enter 1.50
hitting back space makes it 1.5
it should become .15

2) if you need to go back and change the entire amount, you must highlight the value and hit the delete key. It should allow you to highlight and start entering the new amount with out having to hit delete
Amount entered 1.50
If the amount was to be 1.90 and you highlight the value 1.50 and begin to enter 1.90 the value changes to 1,501.90

I have made several attemtps to change and edit with no luck over the corse of the week. So I turn to the collective brain power of the Bytes community.
Below is the script and the form field information. Oh and FYI the script is in a PHP page.


Expand|Select|Wrap|Line Numbers
  1. // JavaScript Document
  2. function currencyFormat(fld, milSep, decSep, e) {
  3.   var sep = 0;
  4.   var key = '';
  5.   var i = j = 0;
  6.   var len = len2 = 0;
  7.   var strCheck = '0123456789';
  8.   var aux = aux2 = '';
  9.   var whichCode = (window.Event) ? e.keyCode : e.which;
  10.   if (whichCode == 8) return true;   // BackSpace
  11.   if (whichCode == 9) return true;   // Tab
  12.   if (whichCode == 46) return true;  // Delete
  13.   key = String.fromCharCode(whichCode);  // Get key value from key code
  14.   if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
  15.   len = fld.value.length;
  16.   for(i = 0; i < len; i++)
  17.   if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
  18.   aux = '';
  19.   for(; i < len; i++)
  20.   if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
  21.   aux += key;
  22.   len = aux.length;
  23.   if (len == 0) fld.value = '';
  24.   if (len == 1) fld.value = '0'+ decSep + '0' + aux;
  25.   if (len == 2) fld.value = '0'+ decSep + aux;
  26.   if (len > 2) {
  27.     aux2 = '';
  28.     for (j = 0, i = len - 3; i >= 0; i--) {
  29.       if (j == 3) {
  30.         aux2 += milSep;
  31.         j = 0;
  32.       }
  33.       aux2 += aux.charAt(i);
  34.       j++;
  35.     }
  36.     fld.value = '';
  37.     len2 = aux2.length;
  38.     for (i = len2 - 1; i >= 0; i--)
  39.     fld.value += aux2.charAt(i);
  40.     fld.value += decSep + aux.substr(len - 2, len);
  41.   }
  42.   return false;
  43. }
Form Field

Expand|Select|Wrap|Line Numbers
  1. <input name="passChg" type="text" value="<? print $_SESSION['passChg']; ?>" style='font:8pt Verdanda, Arial, Helvetica, sans-serif; border:1 solid #FF9F9F;' onFocus="'#ffffCC',;" onBlur="'#ffffff', this.form.passOtChg.value=this.form.passChg.value;" onKeyPress="return(currencyFormat(this,',','.',event))" size="11" maxlength="10" />
Jan 16 '09 #1
Share this Question
Share on Google+
8 Replies

Expert Mod 15k+
P: 16,027
I'm not sure I understand the first problem. How can a backspace key change 1.50 into .15? I've had a brief look at your code and the first problem I notice is return false - nothing will be accepted as a result.
Jan 18 '09 #2

P: 21
I am thinking that since it floats a decimal at 2 places then if there are 3 digits it should show 2.50. Therefore, if you back space and remove the last digit the other two should justify to the right and drop past the decimal and leave a result. This can be skipped I think it is more an issue at this point of not being able to highlight and type over what is there with out having to hit the delete key.

I have been attempting to work it where if you click in the box then it would delete the entry, like you find in some search boxes where there is default text like “Click here to search” and click in the box and the default text disappears…
Jan 18 '09 #3

Expert Mod 15k+
P: 16,027
That could work, but why not make things easier and format/validate onchange?
Jan 18 '09 #4

P: 21
I have tried to add the on change to the text field properties on the form, it nevers seams to fire when I click in the text box. The cursor sits and flashes behind the last digit...if you have a way to make that one work I am all ears.

And thanks for taking a look at this.
Jan 18 '09 #5

Expert Mod 15k+
P: 16,027
onchange fires when you focus off the field and if any changes have been made. What this means is that changes wouldn't happen on key presses. Is that an important requirement?
Jan 18 '09 #6

P: 21
I am not sure for the sake of the form if that would be important...
I think from a user stand point it might be if I am following correctly.


user enters 1.50
user relizes that it should have been 1.90
user clicks in field and enters 190 field shows 1501.90 (since the orginal value is not cleared when they enter the new amount.
using the onchange (again if I am understanding) the user moves out of the field it would then set to 1.90?
Jan 18 '09 #7

Expert Mod 15k+
P: 16,027
No, by changes, I meant validation, formatting, etc., so the user could type whatever they wanted and the validation/formatting would take place once the field loses focus. If you always want the formatting to be correct, then you would have to control by key press, but usually onchange is fine. For example, if the user types 1.5.5, it would be detected when the user focusses off the field. I'm not saying it can't be done on key press, but it does save a lot of headaches, and there's no reason why you can't have a regular expression check to validate.
Jan 18 '09 #8

P: 21
Good point, sorry I was not following along well...The onchange is a good suguestion. I will have to run it by the person that I am working with. They have been adamant that it has to format when being entered...I think that it is because there will be several of these fields (90 or so) and would like to take away the need to input a "." for speed.

I will give it a try and show resutls see if they are game. Thanks for the suguestion and I will post back the results...
Jan 18 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.