<p>Simple js script for price inputs. <ul> <li>Corrects, While typing.</li> <li>Corrects and turns input value to valid price</li> <li>Make value readable.</li> </ul> </p> <input class="priceBox" type="text">
$('input.priceBox').on('priceIt',function(e){ var d=',', s='.', dd=2, v=$(this).val().replace(s,'').split(d), l=v.length; for(var i=0;i<l;i++) { v[i]=v[i].replace(/\D/g,'',10); } if(l>1) { v[l-1]=d+v[l-1].substr(0,dd); } v=v.join(''); if(v.lastIndexOf(d)==0) { v="0"+v; } $(this).data('float',v.replace(d,s)).val(v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g,"$1"+s)); }).on('keyup',function(e){ $(this).trigger('priceIt'); });