Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi...
I am using RAD Text box..I want to check that if user is entering the value in text box it should only character allow spacing its OK. I want it through JavaScript and should display the message box for user that should" enter character only" all this functionality should happen on click of Submit Button...
thanks!!!!!
Posted
Updated 21-Aug-17 22:53pm

JavaScript
<script>
function onkeydown(){
     var ch = String.fromCharCode(event.keyCode);
     var filter = /[a-zA-Z]/   ;
     if(!filter.test(ch)){
          event.returnValue = false;
     }
}
</script>

This can restrict the user to input only characters, but you still have to consider that user can paste numbers, or drag in into textbox.
 
Share this answer
 
Use event.keyCode to accept only char values.
write a function in javascript and call it on onkeypress event of Textbox

JavaScript
function checkNum()
{

if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
   return true;
else
   {
       alert("Please enter only char");
       return false;
   }

}
 
Share this answer
 
Comments
vivek_cool 18-Aug-11 3:28am    
hey thanks
but i want to include space also.. how i can i do this.
i.e. space can allowed
Member 9286545 26-Jul-12 6:52am    
need to pass some value to above function .,,,,,how it could be??
Member 9286545 26-Jul-12 6:53am    
and what is event above?
Member 11512226 11-Mar-16 0:08am    
it doesn't work when i have a text such that number is in between two letters
koolprasad2003 11-Mar-16 0:28am    
this javascript method to be call 'onkeypress' event of textbox, so when you press some key (either number or letter) it should be work as expected, let me know what is the issue

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