1

I am trying to remove text after numbers typed and add decimal

I have multiple input type="text" where on keypress I am adding a comma in INR (Indian Rupee) standard but when I type more than three numbers the entire value is removed and '0' is added. Also my code is not allowing the decimal .00 number as it should. What am I doing wrong?

JS Fiddle

HTML:

<input name="txtMSExMarCardFee" type="number" id="txtMSExMarCardFee" class="Stylednumber">
<input name="txtMSExMarCardFee1" type="number" id="txtMSExMarCardFee1" class="Stylednumber">
<input name="txtMSExMarCardFee2" type="number" id="txtMSExMarCardFee2" class="Stylednumber">

JS:

$('input.Stylednumber').keyup(function(){
    var x=$(this).val();
    x=x.toString();
    var afterPoint = '';
    if(x.indexOf('.') > 0)
    afterPoint = x.substring(x.indexOf('.'),x.length);
    x = Math.floor(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 + afterPoint;


    $(this).val(res );

});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Shaik
  • 930
  • 1
  • 20
  • 48
  • http://stackoverflow.com/questions/16037165/displaying-a-number-in-indian-format-using-javascript – mplungjan Feb 28 '16 at 08:33
  • If you're not sure why this is happening, you shouldn't have programmed it like that. – StackSlave Feb 28 '16 at 08:35
  • @PHPglue you are correct that is why am seeking help to correct it – Shaik Feb 28 '16 at 08:36
  • @mplungjan the link you are suggesting me form there it self i clipped the code but that does not show the example of multiple text box a small suggestion can help me more – Shaik Feb 28 '16 at 08:37

2 Answers2

1

This requires that you cleanup the input before you pass it to through the regex

String.prototype.replaceAll = function(search, replacement) {
  var target = this;
  return target.replace(new RegExp(search, 'g'), replacement);
};

$('input.Stylednumber').keyup(function() {
  var input = $(this).val().replaceAll(',', '');
  if (input.length < 1)
    $(this).val('0.00');
  else {
    var val = parseFloat(input);
    var formatted = inrFormat(input);
    if (formatted.indexOf('.') > 0) {
      var split = formatted.split('.');
      formatted = split[0] + '.' + split[1].substring(0, 2);
    }
    $(this).val(formatted);
  }
});

function inrFormat(val) {
  var x = val;
  x = x.toString();
  var afterPoint = '';
  if (x.indexOf('.') > 0)
    afterPoint = x.substring(x.indexOf('.'), x.length);
  x = Math.floor(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 + afterPoint;
  return res;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="txtMSExMarCardFee" type="text" id="txtMSExMarCardFee" class="Stylednumber">
<input name="txtMSExMarCardFee1" type="number" id="txtMSExMarCardFee1" class="Stylednumber">
<input name="txtMSExMarCardFee2" type="number" id="txtMSExMarCardFee2" class="Stylednumber">
Tah
  • 1,526
  • 14
  • 22
  • perfect thanks for the reply but there is a slight issue that is on tab click to next textbox NaN comes and that is not going cant that be replaced with 0 – Shaik Feb 28 '16 at 08:56
  • Decimal should be fixed in my edit. The NaN occurred when a number wasn't entered and a `keyup` event occurs. – Tah Feb 28 '16 at 09:06
  • cant that nan can be replaced with 0 – Shaik Feb 28 '16 at 09:13
  • Yes, just replace the `$(this).val('0.00');` section with whatever you want it to end up as. – Tah Feb 28 '16 at 09:14
0

Try using this:

$('input.Stylednumber').keyup(function(){
  var t = $(this), v = t.val();
  if(!v.match(/\.\d{2}$/)){
    v += '.00';
  }
  v = parseFloat(v.replace(/,/g, ''));
  if(!v){ // was not a number
    t.val('0.00');
    return false;
  }
  v = v.toFixed(2);
  if(v.length > 6){ 
    t.val(v.slice(0, -6).replace(/(/d{3})/, '$1,')+v.slice(-6));
  }
  else{
    t.val(v);
  }
});
StackSlave
  • 10,613
  • 2
  • 18
  • 35