Skip to content

Instantly share code, notes, and snippets.

@LIQRGV
Created August 8, 2016 01:34
Show Gist options
  • Save LIQRGV/24235d4579b5c406cc7ab245eb5c549e to your computer and use it in GitHub Desktop.
Save LIQRGV/24235d4579b5c406cc7ab245eb5c549e to your computer and use it in GitHub Desktop.
<html>
<body>
Harga: <input class="priceFormat" type="text"/>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('input.priceFormat').keyup(
function(e){
var raw_value = $(e.currentTarget).val();
var value_arr= raw_value.trim().replace(/(\.|,)/g, "").match(/0|[1-9][0-9]+$/g);
if (value_arr == null) {return;}
var value = value_arr[0];
var new_value = "";
var length = value.length;
var counter = length;
var temp = [];
while(counter >= 3)
{
temp.unshift( value.slice(counter-3, counter)); counter -=3 }
if(counter != 0) { temp.unshift( value.slice(0, counter) )
};
new_value = temp.join('.');
$(e.currentTarget).val(new_value);}
)
})
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment