2

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 doesn't work in Mozilla Firefox. How can I solve this problem?

My script is

$(function () {
    $('#name').bind('paste', function () {
        var self = this;
        setTimeout(function () {
            if (!/^[a-zA-Z]+$/.test($(self).val())) $(self).val('');
        }, 0);
    });

    $('#salary').bind('paste', function () {
        var self = this;
        setTimeout(function () {
            if (!/^\d*(\.\d{1,2})+$/.test($(self).val())) $(self).val('');
        }, 0);
    });

    $('.decimal').keypress(function (e) {
        var character = String.fromCharCode(e.keyCode)
        var newValue = this.value + character;
        if (isNaN(newValue) || hasDecimalPlace(newValue, 3)) {
            e.preventDefault();
            return false;
        }
    });

    function hasDecimalPlace(value, x) {
        var pointIndex = value.indexOf('.');
        return  pointIndex >= 0 && pointIndex < value.length - x;
    }
     function isNumberKey(evt) { 
         var charCode = (evt.which) ? 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; 
      } 
});

html is

<b>Name</b>
<input type="text" id="name"  /><br/>
<b>Salary</b>
<input type="text" id="txtChar" onkeypress="return isNumberKey(event)"  name="txtChar" class="CsstxtChar" />
gkiely
  • 2,987
  • 1
  • 23
  • 37
Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84

2 Answers2

4

You should change e.keyCode to e.charCode.

String.fromCharCode(e.charCode)
Parthik Gosar
  • 10,998
  • 3
  • 24
  • 16
4
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" />
Monica
  • 607
  • 4
  • 12
  • 31
  • there should be a ; at the end of " var charCode = (evt.charCode) ? evt.which : event.keyCode "..just a suggestion..upvotted anyway :) – Bhawna Jain Aug 11 '17 at 09:54