Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my java script for restrict alphabet letters

XML
<script language="javascript" type="text/javascript">
        function CheckNumeric(e) {

            if (window.event) // IE
            {
                if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
                    event.returnValue = false;
                    return false;

                }
            }
            else { // Fire Fox
                if ((e.which < 48 || e.which > 57) & e.which != 8) {
                    e.preventDefault();
                    return false;

                }
            }
        }
    </script>




thanks
Posted

1 solution

The key code for the <TAB> is 9, you are catching that.
Excluding it form you're where should fix it.

To awnser you're question better.

The numbers go from keycode 48 to 57.
The alpabeth goes from 56 to 90

So to just include the numbers the if should surround just the alphabet, not all keys.
And if you just wan't letters, include the alpabet

JavaScript
if((e.which >= 48 && e.which <= 57)) {
   //This will only allow alphabet letters & all special keys 
   e.preventDefault();
   return false;
}

if((e.which >= 65 && e.which <= 90)) {
   //This will only allow numbers & all special keys 
   e.preventDefault();
   return false;
}
 
Share this answer
 
v3
Comments
Parazival 29-Mar-15 23:19pm    
The key code for the is 9

sorry i did not understand above line
Sebastiaan Meijerink 30-Mar-15 1:18am    
Sorry, the word TAB should have been there. You are blocking way to many keys. The tab has keycode 9, which is cancelled. So tabindex won't work.
Parazival 30-Mar-15 6:20am    
thankss
Parazival 30-Mar-15 6:21am    
how to write code for only aceepting numbers in java script with tab also

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900