Wednesday 19 September 2012

How to Disable Spacebar in text box using jQuery

For one of my requirement, I need to restrict/disable end-user to enter space in the input field or textbox. This was piece of cake with jQuery. All I need to do is to check the keycode of spacebar and restrict its default action on keydown event. See below jQuery code. "txtNoSpaces" is the name of the textbox.
1$(document).ready(function(){
2  $("#txtNoSpaces").keydown(function(event) {
3     if (event.keyCode == 32) {
4         event.preventDefault();
5     }
6  });
7});
See live Demo and Code.

No comments:

Post a Comment