Yagnik Detroja
Yagnik Detroja

Reputation: 929

Input Masking for indian currency In Jquery dyanamic Input field

I want show masking as per the Indian currency while writing in input box.

I am using InputMask jquery for masking : https://github.com/RobinHerbots/Inputmask

I want that wherever users tries to write down amount in Text Input then it should add automatic comma as per the Indian Currency format.

I am trying to achieve that by using following code.

<input type="text" name="amount" class="amount_field"/>
$('.amount_field').inputmask("numeric", {
            radixPoint: ".",
            groupSeparator: ",",
            digits: 3,
            autoGroup: true,
            prefix: '',
            rightAlign: false,
            allowMinus: false,
            // oncleared: function () { self.Value(''); }
        });

Current Output: 2,500,000

Needed Output : 25,00,000

As per the Indian currency I needed coma first after 3 and then by 2.

Any help will be apprciated.

Upvotes: 4

Views: 2379

Answers (4)

Mayank Dudakiya
Mayank Dudakiya

Reputation: 3869

I have implement your issue with simple following you can try that.

Note: In latest version of InputMask Indian Currency style has been added. Check latest release here. : https://github.com/RobinHerbots/Inputmask/commit/5586f25853e96102417888b2cfc2823f3caa9763

<input type="text" class="inputmasking" value=""/>
<input type="text" class="inputmasking" value=""/>
<input type="text" class="inputmasking" value=""/>

<script type="text/javascript">
Inputmask("(,99){*|1}(,999){1|1}",
     {
         positionCaretOnClick: "radixFocus",
         _radixDance: true,
         radixPoint: ".",
         numericInput: true,
         placeholder: "0"
     }
 ).mask(".inputmasking");
</script>

enter image description here

Working example of fiddle : https://jsfiddle.net/mayanksdudakiya/58pnxjvw/

Upvotes: 4

Rohit.007
Rohit.007

Reputation: 3502

I had created my own. Hope that useful for you too.

$(document).ready(function() {
  $(".amount_field, amount_field2").keyup(function() {
    convertToINRFormat($(this).val(),$(this));
  });
  convertToINRFormat($("#amount_field").val(),$("#amount_field"));
  convertToINRFormat($("#amount_field2").val(),$("#amount_field2"));
});

function convertToINRFormat(value, inputField) {
  var number = Number(value.replace(/,/g, ""));
  // India uses thousands/lakh/crore separators
  $('#result').html(number.toLocaleString('en-IN'));

  $('#result1').html(number.toLocaleString('en-IN', {
    maximumFractionDigits: 2,
    style: 'currency',
    currency: 'INR'
  }));

  $(inputField).val(number.toLocaleString('en-IN'));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="amount" class="amount_field" id="amount_field" value="123456789" />
<input type="text" name="amount" class="amount_field" id="amount_field2" value="987654321" />

<div id="result"></div>
<div id="result1"></div>

Upvotes: 1

Ashu
Ashu

Reputation: 1320

I solved this issue with on change event :

<input type="text" name="masknumber" id="masknumber" onchange="return changeData(this.value);">
<script type="text/javascript">
function changeData(x){
    alert(x);
    x=x.toString();
    var lastThree = x.substring(x.length-3);
    var otherNumbers = x.substring(0,x.length-3);
    if(otherNumbers != '')
    lastThree = ',' + lastThree;
    var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") +  lastThree;
    document.getElementById('masknumber').value = res;
}
</script>

Upvotes: 0

Xun
Xun

Reputation: 375

var number = document.getElementsByClassName("amount_field")[0].value;
number.toLocaleString('en-IN');

Upvotes: 0

Related Questions