不允许在文本框中使用小数

Disallow decimal into textbox not working

本文关键字:小数 文本 不允许      更新时间:2023-09-26

我有一个textbox,用户只能在其中插入numbers。所以我尝试了下面的链接

JS小提琴

但我仍然可以在文本框中插入[.](十进制)。以下是我尝试的

 $(".allownumericwithoutdecimal").on("keypress keyup blur",function (event) {    
       $(this).val($(this).val().replace(/[^'d].+/, ""));
        if ((event.which < 48 || event.which > 57)) {
            event.preventDefault();
        }

文本框

<igtxt:WebNumericEdit ID="txtNoofPages" Width="24%" runat="server" CssClass="allownumericwithoutdecimal">
            </igtxt:WebNumericEdit>

我试过JS Fiddle,它可以很好地与Firefox、IE和chrome浏览器配合使用。然后我用同样的正则表达式JQuery尝试了这个,它运行得很好。

$("#onlyNumber").on('keypress keyup blur', function(){
     $(this).val($(this).val().replace(/[^'d].+/, ""));
        if ((event.which < 48 || event.which > 57)) {
            event.preventDefault();
        }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Number Only</label>
<input type='textbox' id='onlyNumber'>