We help IT Professionals succeed at work.
Get Started

onKeyUp - Format currency - Can't copy/paste

digitalwise
digitalwise asked
on
106 Views
Last Modified: 2015-12-28
I have a form field

<input type="Text"  id="DQuestion_123" value="" size="15" maxlength="20" onkeyup="formatCurrency(this, event);"  /> 

Open in new window


I cannot copy/paste into this field.   Even if it is 1231231.     The formatCurrency is pretty old.   Any chance of salvaging this or should I just go and change it to out to a new jquery format??

var oldValue = [];
 
   var VK_BACK     = 0x08;      // BACKSPACE key
   var VK_TAB      = 0x09;      // TAB key 
   var VK_SHIFT    = 0x10;      // SHIFT key 
   var VK_CONTROL  = 0x11;      // CTRL key 
   var VK_MENU     = 0x12;      // ALT key 
   var VK_END      = 0x23;      // END key 
   var VK_HOME     = 0x24;      // HOME key 
   var VK_LEFT     = 0x25;      // LEFT ARROW key 
   var VK_RIGHT    = 0x27;      // RIGHT ARROW key 
   var VK_DELETE   = 0x2E;      // DEL key 
   var VK_0        = 0x30;      // 0 key
   var VK_1        = 0x31;      // 1 key
   var VK_2        = 0x32;      // 2 key
   var VK_3        = 0x33;      // 3 key
   var VK_4        = 0x34;      // 4 key
   var VK_5        = 0x35;      // 5 key
   var VK_6        = 0x36;      // 6 key
   var VK_7        = 0x37;      // 7 key
   var VK_8        = 0x38;      // 8 key
   var VK_9        = 0x39;      // 9 key
   var VK_NUMPAD0  = 0x60;      // Numeric keypad 0 key
   var VK_NUMPAD1  = 0x61;      // Numeric keypad 1 key
   var VK_NUMPAD2  = 0x62;      // Numeric keypad 2 key
   var VK_NUMPAD3  = 0x63;      // Numeric keypad 3 key
   var VK_NUMPAD4  = 0x64;      // Numeric keypad 4 key
   var VK_NUMPAD5  = 0x65;      // Numeric keypad 5 key
   var VK_NUMPAD6  = 0x66;      // Numeric keypad 6 key
   var VK_NUMPAD7  = 0x67;      // Numeric keypad 7 key
   var VK_NUMPAD8  = 0x68;      // Numeric keypad 8 key
   var VK_NUMPAD9  = 0x69;      // Numeric keypad 9 key
   var VK_SUBTRACT = 0x6D;      // Subtract key 
   var VK_DECIMAL  = 0x6E;      // Decimal key 
   var VK_HYPHEN   = 0xBD;      // Windows 2000/XP: For any country/region, the '-_' key
   var VK_PERIOD   = 0xBE;      // Windows 2000/XP: For any country/region, the '.>' key
 
   function formatCurrency(element, __event)
   {
      if (oldValue[element.id] == undefined)
      {
         oldValue[element.id] = "";
      }
      var keyCode;
      if (window.event) // IE
      {
         keyCode = __event.keyCode;
      }
      else if (__event.which) // Netscape/Firefox/Opera
      {
         keyCode = __event.which;
      }
      var shiftDown = __event.shiftKey;
      var ctrlDown  = __event.ctrlKey;
      var altDown   = __event.altKey;
      var validChar = 
      (
         (
            (
               (
                  ((keyCode >= VK_0)       && (keyCode <= VK_9))       || 
                  ((keyCode >= VK_NUMPAD0) && (keyCode <= VK_NUMPAD9))
               ) || 
               (
                  (keyCode == VK_HYPHEN)   || 
                  (keyCode == VK_SUBTRACT)
               ) || 
               (
                  (keyCode == VK_PERIOD)  || 
                  (keyCode == VK_DECIMAL)
               ) || 
               (
                  (keyCode == VK_DELETE) || 
                  (keyCode == VK_BACK)
               )
            ) && !shiftDown && !ctrlDown && !altDown
         ) || 
         (
            (
               (keyCode == VK_HOME)  || 
               (keyCode == VK_END)   || 
               (keyCode == VK_LEFT)  || 
               (keyCode == VK_RIGHT)
            )
         ) && !ctrlDown && !altDown
      );
      if (validChar)
      {
         if ((keyCode == VK_HYPHEN) || (keyCode == VK_SUBTRACT))
         {
            if (element.value.indexOf("-") != 0)
            {
               element.value = oldValue[element.id];
            }
            return false;
         }
         else if ((keyCode == VK_PERIOD) || (keyCode == VK_DECIMAL))
         {
            if (element.value.indexOf(".") < (element.value.length - 1))
            {
               element.value = oldValue[element.id];
            }
            return false;
         }
         else if ((keyCode == VK_HOME) || (keyCode == VK_END) || (keyCode == VK_LEFT) || (keyCode == VK_RIGHT) || (keyCode == VK_DELETE))
         {
            return false;
         }
		 else if (keyCode == VK_BACK)
         {
         }
      }
      else
      {
         if ((keyCode != VK_SHIFT) && (keyCode != VK_CONTROL) && (keyCode != VK_MENU) && (keyCode != VK_TAB))
         {
            element.value = oldValue[element.id];
         }
         return false;
      }
      var number     = moneyToNumber(element.value);
      var negative   = ((""+number).substr(0, 1) == "-");
      number         = number.replace("-", "");
      var floatArray = number.split(".");
      floatArray[0]  = "" + parseFloat(floatArray[0]);
      var newValue   = addCommas(floatArray[0]);
      if (floatArray[1] != undefined)
      {
         newValue += "." + floatArray[1].substr(0, 2);
      }
      if (negative)
      {
         newValue = "-" + newValue;
      }
      if (element.value != newValue)
      {
         element.value        = newValue;
         oldValue[element.id] = newValue;
      }
      return false;
   }
 
   function addCommas(number)
   {
      return ((number.length > 3) ? (addCommas(number.substring(0, number.length - 3)) + "," + number.substring(number.length - 3, number.length)) : 
         String(number));
   }
 
   function moneyToNumber(money)
   {
      var number = money.replace("$", "");
	  if (number.indexOf(".") == 0) {
		number = '0' + number;
	  }

      while (number.indexOf(",") > -1)
      {
         number = number.replace(",", "");
      }
          if ((number.length)<1) return 0; else return number;
   }

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE