keypress is not working in Mozila Firefox

后端 未结 2 1058
有刺的猬
有刺的猬 2021-01-22 17:04

I have got a task to restrict textbox from alphabetic values. Only floating point values are possible. After the decimal we have to write out two digits. I did this but it does

相关标签:
2条回答
  • 2021-01-22 18:01

    You should change e.keyCode to e.charCode.

    String.fromCharCode(e.charCode)
    
    0 讨论(0)
  • 2021-01-22 18:05
    function isNumberKey(evt) { 
         var charCode = (evt.charCode) ? evt.which : event.keyCode
    
         if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) 
             return false; 
         else { 
             var len = document.getElementById("txtChar").value.length; 
             var index = document.getElementById("txtChar").value.indexOf('.'); 
    
             if (index > 0 && charCode == 46) { 
                 return false; 
             } 
                 if (index >0 || index==0) { 
                     var CharAfterdot = (len + 1) - index; 
                     if (CharAfterdot > 3) { 
    
                         return false; 
                     } 
    
    }
    
             } 
             return true; 
          } 
    
    
    
    
    
    <input type="text" id="txtChar" onkeypress="return isNumberKey(event)"  name="txtChar" class="CsstxtChar" />
    
    0 讨论(0)
提交回复
热议问题