jQuery - Allow only two decimal values -


code:

<input onkeypress="return isnumberkey(event)" type="number" value="" />  function isnumberkey(evt){     var charcode = (evt.which) ? evt.which : event.keycode     if (charcode > 31 && (charcode < 48 || charcode > 57) && charcode != 46){            return false;     }        return true; } 

the above function allows user enter value more 2 decimal i.e. 10.5678. how can modify function , restrict user enter values upto 2 decimal i.e. 10.56

try below code give class number element

$('.number').on('keypress',function (event) {     if ((event.which != 46 || $(this).val().indexof('.') != -1) && (event.which < 48 || event.which > 57)) {         event.preventdefault();     }     var input = $(this).val();     if ((input.indexof('.') != -1) && (input.substring(input.indexof('.')).length > 2)) {         event.preventdefault();     } }); 

and javascript

function isnumberkey(evt){      console.log(evt.value);    if ((evt.which != 46 || evt.value.indexof('.') != -1) && (evt.which < 48 || evt.which > 57)) {          //event it's fine        }      var input = evt.value;      if ((input.indexof('.') != -1) && (input.substring(input.indexof('.')).length > 2)) {          return false;      }  }
<input onkeypress="return isnumberkey(this)" type="number" value="" />


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -