Problem
You have not passed the event into the function.
Solution
1. You need to do like below.
<asp:TextBox ID="txtEmpName"
runat="server"
onkeypress="return AvoidSpace(event)">
</asp:TextBox>
2. And use this event in the function. Modify the function as below.
function AvoidSpace(event) {
var k = event ? event.which : window.event.keyCode;
if (k == 32) return false;
}
Quote:
The onkeypress event is not fired for all key types in all browsers. For details, please see the table below.
To get the pressed key, use the keyCode, charCode and which event properties.
Refer -
onkeypress event | keypress event[
^].
window.event.keyCode
is for Internet Explorer.
event.which
is for other browsers.
DEMO
onkeypress Event Demo - restrict space key[
^].