最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - allowing tab key when we are allowing only numbers in textbox using java script - Stack Overflow

programmeradmin1浏览0评论

i written a java script which allows only numbers,ma,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)

i written a java script which allows only numbers,ma,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)

Share Improve this question edited Feb 6, 2014 at 1:49 vishnu reddy asked Feb 5, 2014 at 11:17 vishnu reddyvishnu reddy 1231 gold badge5 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论