0

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

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

Paritosh
  • 11,144
  • 5
  • 56
  • 74
Monica
  • 607
  • 4
  • 12
  • 31

1 Answers1

0

PLease follow this question of mine. My requirement was of 2 decimal places

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

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

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

Community
  • 1
  • 1
Ankur
  • 1,023
  • 7
  • 27
  • 42