Restrict Space and Special Character using Javascript

  1. function RestrictSpaceSpecial(e) {  
  2.     var k;  
  3.     document.all ? k = e.keyCode : k = e.which;  
  4.     return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));  

Now access the JavaScript function on keypress event of textbox as shown below:
  1. <asp:TextBox ID="TextBox2" runat="server" onkeypress="RestrictSpaceSpecial();" />