javascript - Number on keyup add comma in INR standard -
i trying remove text after numbers typed , add decimal
i have multiple input type="text"
on keypress adding comma in inr (indian rupee) standard when type more 3 numbers entire value removed , '0' added. code not allowing decimal .00 number should. doing wrong?
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 ); });
this requires cleanup input before pass through 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">
Comments
Post a Comment