1

i written a java script which allows only numbers,comma,dot. i applied it on four text boxes. my issue is i have 10 text boxes which takes different types of data on which four of them i applied java script. i can use tab key on other text boxes but i am not able to use on java script applied text boxes to move courser. is there any change that i have to do in my script... Thanks.

Java Script:-

function isNumberCommaDot(evt) {
         var theEvent = evt || window.event;
         var key = theEvent.keyCode || theEvent.which;
         key = String.fromCharCode(key);
         if (key.length == 0) return;
         var regex = /^[0-9,\9\b]*\.?[0-9]*$/;
         if (!regex.test(key)) {
             theEvent.returnValue = false;
             if (theEvent.preventDefault) theEvent.preventDefault();
         }
     }

i used \9 in regex but still its not accepting tab key.(9 is ASCII char. for TAB Key)

vishnu reddy
  • 103
  • 1
  • 4
  • 9

1 Answers1

7

You can check whether it was a tab press earlier, and just skip processing

function isNumberCommaDot(evt) {
     var theEvent = evt || window.event;
     var key = theEvent.keyCode || theEvent.which;

     if (key === 9 ) { //TAB was pressed
        return;
     }

     key = String.fromCharCode(key);
     if (key.length == 0) return;
     var regex = /^[0-9,\9\b]*\.?[0-9]*$/;
     if (!regex.test(key)) {
         theEvent.returnValue = false;
         if (theEvent.preventDefault) theEvent.preventDefault();
     }
 }

You can find more info here

Community
  • 1
  • 1
Szilard Muzsi
  • 1,881
  • 2
  • 16
  • 20