por haí:
Cita: function validar(e) {
tecla = (document.all)?e.keyCode:e.which;
if (tecla==8) return true;
patron=/^\w+$/;
te = String.fromCharCode(tecla);
if(patron.test(te)){
return true;
}else{
alert("caracter no permitido");
return false;
}
}
aunque el código genera un alert en el caso de que por ejemplo se precione la tecla shift.
yo lo aría así:
Cita: <html>
<head>
<script>
function validar(i) {
patron=/[^a-zA-Z0-9-]/;
if(patron.test(i.value)){
alert("caracter no permitido");
i.value=i.value.replace(patron,"");
}
}
</script>
</head>
<body>
<input type="text" name="textfield" onkeyup="validar(this)">
</body>
</html>
claro que cada uno tiene su forma de hacer las cosas.
saludos