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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -