2
Answers

only entering letters/characters into textbox

Carol Cashman

Carol Cashman

7y
213
1
hey guys
 
I want to restrict a textbox to only entering characters (a-z)
 
i have this done for integers but can figure out a way to do it for characters
 
 
thanks guys
Answers (2)
1
Ajeesh

Ajeesh

NA 349 1.6k 7y
You can use below code to make textbox accepts only alphabets
  1. <script>    
  2.      $(document).ready(function() {    
  3.        //Attach key press event to textbox    
  4.        $("#Textbox1").keypress(function(e) {    
  5.          //Get the key code    
  6.          var key = e.keyCode;    
  7.          if (key >= 48 && key <= 57) {    
  8.            e.preventDefault();    
  9.          }    
  10.        });    
  11.      });    
  12.    
  13.    </script>    
Complete Code 
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4.   <head>  
  5.     <meta charset="utf-8">  
  6.     <meta name="viewport" content="width=device-width">  
  7.     <script src="https://code.jquery.com/jquery-1.9.1.js"></script>  
  8.   
  9.     <script>  
  10.       $(document).ready(function() {  
  11.         //Attach key press event to textbox  
  12.         $("#Textbox1").keypress(function(e) {  
  13.           //Get the key code  
  14.           var key = e.keyCode;  
  15.           if (key >= 48 && key <= 57) {  
  16.             e.preventDefault();  
  17.           }  
  18.         });  
  19.       });  
  20.   
  21.     </script>  
  22.   </head>  
  23.   
  24.   <body>  
  25.     <input type="text" id="Textbox1" />  
  26.   </body>  
  27.   
  28. </html>  
 Sample Demo : http://jsbin.com/cuyubofiki/edit?html,output
Accepted
1
Rupesh Kahane

Rupesh Kahane

NA 10.6k 866.5k 7y
Try this 
  1. <html>  
  2.   
  3. <head>  
  4.   
  5.     <title>allwon only alphabets in textbox using JavaScript</title>  
  6.   
  7.     <script language="Javascript" type="text/javascript">  
  8.   
  9.    
  10.   
  11.         function onlyAlphabets(e, t) {  
  12.   
  13.             try {  
  14.   
  15.                 if (window.event) {  
  16.   
  17.                     var charCode = window.event.keyCode;  
  18.   
  19.                 }  
  20.   
  21.                 else if (e) {  
  22.   
  23.                     var charCode = e.which;  
  24.   
  25.                 }  
  26.   
  27.                 else { return true; }  
  28.   
  29.                 if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))  
  30.   
  31.                     return true;  
  32.   
  33.                 else  
  34.   
  35.                     return false;  
  36.   
  37.             }  
  38.   
  39.             catch (err) {  
  40.   
  41.                 alert(err.Description);  
  42.   
  43.             }  
  44.   
  45.         }  
  46.   
  47.    
  48.   
  49.     </script>  
  50.   
  51. </head>  
  52.   
  53. <body>  
  54.   
  55.     <table align="center">  
  56.   
  57.         <tr>  
  58.   
  59.             <td>  
  60.   
  61.                 <input type="text" onkeypress="return onlyAlphabets(event,this);" />  
  62.   
  63.             </td>  
  64.   
  65.         </tr>  
  66.   
  67.     </table>  
  68.   
  69. </body>  
  70.   
  71. </html>