Real time special character check validation

Hello all,

I’ve got the following function which check for any special character into an input field.

<script type=“text/javascript”>
function chkSpecialChars(input)
{
var iChars = “!@#$%^&*()+=-\\\';,./{}|\”:<>?"; for (var i = 0; i < input.length; i++)
{
if (iChars.indexOf(input.charAt(i)) != -1)
{
alert (“You are using special characters.
These are not allowed.
Please remove them and try again or leave the field empty, it will be automatically filled.”);
return false;
}
}
return true;
}
</script>

What I want is to display the above message when a user just move his/her mouse away from that input field and not when clicking the submit button or even better doing a real time check while user typing.

I’ve tried various combinations like the below but non worked.

<input name=“title_alias” type=“text” id=“title” value=“{$title_alias|escape}” size=“53” onkeypress=“return chkSpecialChars()” />

Any help is much appreciated.

Thanks