troubleshooting Question

onKeyUp - Format currency - Can't copy/paste

Avatar of digitalwise
digitalwise asked on
JavaScript
2 Comments1 Solution109 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
dgrafx

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 2 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 2 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004