Try the following
<asp:TextBox ID="txtCode" runat="server" Width="200px" Enabled="true" OnKeyPress="return restrictCharacters(event);"></asp:TextBox>
and the below JavaScript function
function restrictCharacters(e) {
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
var alphaOnly = /[A-Za-z0-9]/g;
if (code == 27) { this.blur(); return false; }
if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40) {
if (character.match(alphaOnly)) {
return true;
} else {
return false;
}
}
}
Hope this helps.