Allowing only numbers and one decimal

后端 未结 6 1928
醉梦人生
醉梦人生 2020-12-19 17:58

Guys and gals i have this piece of JavaScript code that only allows for numbers and one decimal period. The problem i\'m having is that when i tab over to my textbox contro

6条回答
  •  情深已故
    2020-12-19 18:59

    Here is a sample solution that will accept a number with one(1) decimal point only. e.g 1.12, 11.5

    Enter a number with one(1) decimal point only

    $('.decimalPt').keypress(function(evt) { evt = (evt) ? evt : window.event; var charCode = (evt.which) ? evt.which : evt.keyCode; if (charCode == 8 || charCode == 37) { return true; } else if (charCode == 46 && $(this).val().indexOf('.') != -1) { return false; } else if (charCode > 31 && charCode != 46 && (charCode < 48 || charCode > 57)) { return false; } return true; });

    Take a look at this: https://jsfiddle.net/sudogem/h43r6g7v/12/

提交回复
热议问题