how to validate, to enter negative and positive decimal values upto 2 decimal places using jquery keypress -
i have grid in have allow user enter either positive or negative value upto 2 decimal places.
//on keypress event amountvalidation: function (e) { $('.js-amt').keypress(function (e) { var character = string.fromcharcode(e.keycode) var newvalue = this.value + character; if (isnan(newvalue) || hasdecimalplace(newvalue, 3)) { e.preventdefault(); return false; } }); function hasdecimalplace(value, x) { pointindex = value.indexof('.'); return pointindex >= 0 && pointindex < value.length - x; } },
you can use:
$('.js-amt').keypress(function (e) { var regex = /^-?\d+(\.\d{0,2})?$/g; if (!regex.test(this.value)) { this.value = ''; }});
you can use:
$('.js-amt').keypress(function (e) { $(this).val(function(i,val){ return parsefloat(val,10).tofixed(2) }); });
Comments
Post a Comment