简体   繁体   English

文本框允许数字为小数点后一位,小数点后一位

[英]textbox allow a number with one decimal point, and one digit after decimal point

I want to restrict a textbox value to a number with one decimal point, and one digit after decimal point.for eg:4.5,34.5,.4 我想将文本框值限制为一个小数点后一位数字,例如:4.5,34.5,.4

Here is my code 这是我的代码

 function isNumberKey(obj,evt) { 
         var charCode = (evt.charCode) ? evt.which : event.keyCode

         if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) 
             return false; 
         else { 
         var input = obj.value;
             var len = obj.value.length; 
             var index = obj.value.indexOf('.'); 
             if (index > 0 && charCode == 46) { 
                 return false; 
             } 
             if (index >0 || index==0) { 
                 var CharAfterdot = (len + 1) - index; 
                 if (CharAfterdot > 2) { 

                     return false; 
                 } 

        }

    if (charCode == 46 && input.split('.').length >1) {
        return false;
        }


         } 
         //return true; 
      } 
   <ItemTemplate>                                                    
 <asp:TextBox ID="txt1" runat="server" Width="50px" onkeypress="return isNumberKey(this,event);" maxlength="4" Style="text-align: right;"></asp:TextBox>
</ItemTemplate>  

Using this in asp.net c#.this will be properly working in chrome and mozilla,but not working in IE 在asp.net c#中使用它。这将在chrome和mozilla中正常工作,但在IE中不工作

PLease follow this question of mine. 请遵循我的这个问题。 My requirement was of 2 decimal places 我的要求是2位小数

how to limit only two digits after decimal point using Regex Validator 如何使用正则表达式验证器仅限制小数点后两位

same question after scrolling down you will get 2 links as follows..they will help you 向下滚动后的相同问题,您将获得2个链接,如下所示。它们将为您提供帮助

http://www.mredkj.com/tutorials/validate2.html http://www.mredkj.com/tutorials/validate2.html

http://www.mredkj.com/tutorials/validate2.js http://www.mredkj.com/tutorials/validate2.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM