Se vuoi che il controllo avvenga durante la digitazione devi utilizzare l'evento onkeypress:
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Esempio</title>
<script type="text/javascript">
function normalOnly(oToCheckField, oKeyEvent) {
var nChar = (oKeyEvent || /* IE */ window.event || { charCode: 0 }).charCode;
return nChar === 0 || /^[0-9a-zA-Z\s]+$/.test(String.fromCharCode(nChar));
}
</script>
</head>
<body>
<form name="myForm">
Solo numeri e lettere: <input type="text" name="myInput" onkeypress="return(normalOnly(this, event));" onpaste="return(false);" /></p>
</form>
</body>
</html>
Se vuoi eliminare la possibilità di inserire gli spazi, devi cancellare i due caratteri "\s" in rosso nel codice, come ti diceva TrueLies.