5

I have input box with condition to allow only numeric. While adding numeric automatically add DOTs as currency format like following.

100 = 100 1000 = 1.000 10000 = 10.000 1000000 = 100.000 1000000 = 1.000.000 <input type="text" class="form-control" name="number_one" id="number_one" onkeypress="return event.charCode >= 48 && event.charCode <= 57" onkeyup="return CurrencyFormat(this.value)"> 

Can anyone please guide me how to call function value.

I found following useful but How to modify this function to automatically to add Dots into input box value and condition to only allow numbers.

function getNumberWithCommas(number) { return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); } 

Thank you.

2 Answers 2

6

Check following snippet demo.

$('input.numberformat').keyup(function(event) { // skip for arrow keys if(event.which >= 37 && event.which <= 40) return; // format number $(this).val(function(index, value) { return value .replace(/\D/g, "") .replace(/\B(?=(\d{3})+(?!\d))/g, ".") ; }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input class="numberformat">

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure what you are trying to do, but to have the function called on the element value you can have set onchange attribute, but if you already have a function that handle onchange and want to integrate it with this function please add to your question too and I will amend my answer accordingly.

function CurrencyFormat(el) { let val = el.value.split('.').join(''); el.value = val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); }
<input type="text" class="form-control" name="number_one" id="number_one" onkeypress="CurrencyFormat(this)" >

5 Comments

For allowing only number I am using following way <input type="text" class="form-control" name="number_one" id="number_one" onkeypress="return event.charCode >= 48 && event.charCode <= 57" onkeyup="return CurrencyFormat(this.value)">
@VipulGhetiya, its working as a snippet, not sure what not working, will check the number value in minutes.
Thanks ... I modified your function little bit like this prntscr.com/rl3i2n We are close enough
Thanks for your snippet above. Instead of "OnChange" event it makes change only when we go to next input field. Can you please modify to OnKeyPress or something. By doing that we can check live updates of values
Ok sure, will amend it now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.