javascript - Jquery price format with lakhs separator? -


i got jquery price format jquery plugin, working thousand separators (ex: 32'32,232.33), need lakhs separator thousands (32'32,232.33) , have altered jquery seems work lakhs not working lakhs thousands also

format.js

/*  * price format jquery plugin * created eduardo cuducos cuducos [at] gmail [dot] com * maintained flavio silveira flavio [at] gmail [dot] com * version: 1.7 * release: 2012-02-22  * original char limit flavio silveira <http://flaviosilveira.com> * original keydown event attachment kaihua qi * keydown fixes thasmo <http://thasmo.com> * clear prefix on blur suggest ricardo mendes phonoway * original allow negative cagdas ucar <http://carsinia.com> * keypad fixes carlos vinicius <http://www.kvinicius.com.br> , rayron victor * original suffix marlon pires junior * centslimit set 0 fixed jereon de jong * original idea use of plus sign  */  (function($) {      /****************     * main function *     *****************/     $.fn.priceformat = function(options)     {          var defaults =         {             prefix: 'us$ ',             suffix: '',             centsseparator: '.',             thousandsseparator: ',',            lakhsseparator: "'",             limit: false,             centslimit: 2,             clearprefix: false,             clearsufix: false,             allownegative: false,             insertplussign: false         };          var options = $.extend(defaults, options);          return this.each(function()         {              // pre defined options             var obj = $(this);             var is_number = /[0-9]/;              // load pluggings settings             var prefix = options.prefix;             var suffix = options.suffix;             var centsseparator = options.centsseparator;             var thousandsseparator = options.thousandsseparator;             var lakhsseparator = options.lakhsseparator;             var limit = options.limit;             var centslimit = options.centslimit;             var clearprefix = options.clearprefix;             var clearsuffix = options.clearsuffix;             var allownegative = options.allownegative;             var insertplussign = options.insertplussign;              // if insertplussign on, automatic turns on allownegative, work signs             if (insertplussign) allownegative = true;              // skip isn't number             // , skip left zeroes             function to_numbers (str)             {                 var formatted = '';                 (var i=0;i<(str.length);i++)                 {                     char_ = str.charat(i);                     if (formatted.length==0 && char_==0) char_ = false;                      if (char_ && char_.match(is_number))                     {                         if (limit)                         {                             if (formatted.length < limit) formatted = formatted+char_;                         }                         else                         {                             formatted = formatted+char_;                         }                     }                 }                  return formatted;             }              // format fill zeros complete cents chars             function fill_with_zeroes (str)             {                 while (str.length<(centslimit+1)) str = '0'+str;                 return str;             }              // format price             function price_format (str)             {                 // formatting settings                 var formatted = fill_with_zeroes(to_numbers(str));                 var thousandsformatted = '';                 var thousandscount = 0;                 var lakhsformatted = '';                 var lakhscount = 0;                 // checking centslimit                 if(centslimit == 0)                 {                     centsseparator = "";                     centsval = "";                 }                  // split integer cents                 var centsval = formatted.substr(formatted.length-centslimit,centslimit);                 var integerval = formatted.substr(0,formatted.length-centslimit);                  // apply cents pontuation                 formatted = (centslimit==0) ? integerval : integerval+centsseparator+centsval;                  // apply thousands pontuation                   if (thousandsseparator || $.trim(thousandsseparator) != "")                 {                     (var j=integerval.length;j>0;j--)                     {                         char_ = integerval.substr(j-1,1);                         thousandscount++;                         if (thousandscount%3==0) char_ = thousandsseparator+char_;                         thousandsformatted = char_+thousandsformatted;                     }                      //                     if (thousandsformatted.substr(0,1)==thousandsseparator) thousandsformatted = thousandsformatted.substring(1,thousandsformatted.length);                     formatted = (centslimit==0) ? thousandsformatted : thousandsformatted+centsseparator+centsval;                 }                  // apply lakhs pontuation              if (lakhsseparator || $.trim(lakhsseparator) != "")                 {                      (var j=integerval.length;j>0;j--)                     {                         char_ = integerval.substr(j-1,1);                         char1_ = integerval.substr(j-1,1);                         lakhscount++;                              if (lakhscount%5==0)                      char_ =lakhsseparator+char_;                         lakhsformatted = char_+lakhsformatted;                       }                       if (lakhsformatted.substr(0,1)==lakhsseparator) lakhsformatted = lakhsformatted.substring(1,lakhsformatted.length);                     formatted = (centslimit==0) ? lakhsformatted : lakhsformatted+centsseparator+centsval;                 }                   // if string contains dash, negative - add begining (except zero)                 if (allownegative && str.indexof('-') != -1 && (integerval != 0 || centsval != 0))                     formatted = '-' + formatted;                 else if (insertplussign && (integerval != 0 || centsval != 0))                     formatted = '+' + formatted;                  // apply prefix                 if (prefix) formatted = prefix+formatted;                  // apply suffix                 if (suffix) formatted = formatted+suffix;                  return formatted;             }              // filter user type (only numbers , functional keys)             function key_check (e)             {                 var code = (e.keycode ? e.keycode : e.which);                 var typed = string.fromcharcode(code);                 var functional = false;                 var str = obj.val();                 var newvalue = price_format(str+typed);                  // allow key numbers, 0 9                 if((code >= 48 && code <= 57) || (code >= 96 && code <= 105)) functional = true;                  // check backspace, tab, enter, delete, , left/right arrows                 if (code ==  8) functional = true;                 if (code ==  9) functional = true;                 if (code == 13) functional = true;                 if (code == 46) functional = true;                 if (code == 37) functional = true;                 if (code == 39) functional = true;                 // minus sign, plus sign                 if (allownegative && (code == 189 || code == 109)) functional = true; // dash                 if (insertplussign && (code == 187 || code == 107)) functional = true;                  if (!functional)                 {                     e.preventdefault();                     e.stoppropagation();                     if (str!=newvalue) obj.val(newvalue);                 }              }              // inster formatted price value of input field             function price_it ()             {                 var str = obj.val();                 var price = price_format(str);                 if (str != price) obj.val(price);             }              // add prefix on focus             function add_prefix()             {                 var val = obj.val();                 obj.val(prefix + val);             }              function add_suffix()             {                 var val = obj.val();                 obj.val(val + suffix);             }              // clear prefix on blur if set true             function clear_prefix()             {                 if($.trim(prefix) != '' && clearprefix)                 {                     var array = obj.val().split(prefix);                     obj.val(array[1]);                 }             }              // clear suffix on blur if set true             function clear_suffix()             {                 if($.trim(suffix) != '' && clearsuffix)                 {                     var array = obj.val().split(suffix);                     obj.val(array[0]);                 }             }              // bind actions             $(this).bind('keydown.price_format', key_check);             $(this).bind('keyup.price_format', price_it);             $(this).bind('focusout.price_format', price_it);              // clear prefix , add prefix             if(clearprefix)             {                 $(this).bind('focusout.price_format', function()                 {                     clear_prefix();                 });                  $(this).bind('focusin.price_format', function()                 {                     add_prefix();                 });             }              // clear suffix , add suffix             if(clearsuffix)             {                 $(this).bind('focusout.price_format', function()                 {                     clear_suffix();                 });                  $(this).bind('focusin.price_format', function()                 {                     add_suffix();                 });             }              // if value has content             if ($(this).val().length>0)             {                 price_it();                 clear_prefix();                 clear_suffix();             }          });      };      /**********************     * remove price format *     ***********************/     $.fn.unpriceformat = function(){       return $(this).unbind(".price_format");     };      /******************     * unmask function *     *******************/     $.fn.unmask = function(){          var field = $(this).val();         var result = "";          for(var f in field)         {             if(!isnan(field[f]) || field[f] == "-") result += field[f];         }          return result;     };  })(jquery); 

index.php

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript" src="format.js"></script>  <script type="text/javascript">              $(function(){                    $('#price').priceformat({                       prefix: '',                      thousandsseparator: ',',                     lakhsseparator: "'"                 });  $('#price1').priceformat({                       prefix: '',                      thousandsseparator: ''                 });                 });            </script> </head>  <body> <input type="text" id="price"> </body> </html> 

why not working ? kindly advise me.

you have add thousandscount%5==0 original http://jquerypriceformat.com/txt/jquery.price_format.1.8.js_.txt lakhs count

if (thousandscount%3==0 || thousandscount%5==0 ) char_ = thousandsseparator+char_;                         thousandsformatted = char_+thousandsformatted; 

this code doing.

for example number 1234567.89

  1. with thousands formatting the number changes 1,234,67.89
  2. again in step 2,your code again formatting number 12,34567.89 not taking number 1,234,67.89 input original integer value 1234567
  3. in return getting output 12,34567.89 , not desired output 1,2,34,567.89 ( ofcourse question asking )

Comments