trollolodn

#jquery: format number onload and onkeyup input html

Apr 18th, 2013
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Using js lib NumberFormat, download in http://www.mredkj.com/javascript/nf_api.html
  2.  
  3. $(function(){
  4. //Format number for all controls $('input.only_number') onload action of page
  5. var arr2 = $('input.only_number');
  6.       var arrNumber2 = new Array();
  7.       for(var i = 0; i< arr2.length;i++){
  8.         arrNumber2[i] = formatMoney($(arr2[i]).val());
  9.         $(arr2[i]).val(arrNumber2[i]);
  10.       }
  11.  
  12. //Format number when onkeyup action
  13.       $('input.only_number').keyup(function(){
  14.         //To format number separator by ","
  15.         num = $(this).val();
  16.         num = new NumberFormat(num);
  17.         num.setPlaces('0', false);
  18.         num = num.toFormatted();
  19.         $(this).val(num);
  20.  
  21.         //Save number to hidden input >> It helps you to post data to the server properly format
  22.         save_num = new NumberFormat(num);
  23.         save_num = save_num.toUnformatted();
  24.         $(this).next().val(save_num);
  25.       });
  26. });
Advertisement
Add Comment
Please, Sign In to add comment