trollolodn icon

#jquery: format number onload and onkeyup input html

trollolodn | PRO | 04/18/13 07:46:17 AM UTC | 0 ⭐ | 281 👁️ | Never ⏰ | []
JavaScript |

920 B

|

None

|

0 👍

/

0 👎

//Using js lib NumberFormat, download in http://www.mredkj.com/javascript/nf_api.html
 
$(function(){
//Format number for all controls $('input.only_number') onload action of page
var arr2 = $('input.only_number');
      var arrNumber2 = new Array();
      for(var i = 0; i< arr2.length;i++){
        arrNumber2[i] = formatMoney($(arr2[i]).val());
        $(arr2[i]).val(arrNumber2[i]);
      }
 
//Format number when onkeyup action
      $('input.only_number').keyup(function(){
        //To format number separator by ","
        num = $(this).val();
        num = new NumberFormat(num);
        num.setPlaces('0', false);
        num = num.toFormatted();
        $(this).val(num);
 
        //Save number to hidden input >> It helps you to post data to the server properly format
        save_num = new NumberFormat(num);
        save_num = save_num.toUnformatted();
        $(this).next().val(save_num);
      });
});

Comments