how to detect the Enter-key in a text box ?
Hi, I am new to javascript and just working my way through 'Beginning Javascript' so bear with me. If you hit the Enter-key in a text box having entered several characters, the text vanish and any validation of the content of the text box is not possible (at least to me).
I have tried to detect the Enter-key (and other white space characters) like this:
.................................................. ....
<html>
<head>
<title>ENTER_test</title>
<script type="text/javascript" language="Javascript">
<!--
function checkKey()
{
var str=document.form1.tekst1.value;
// alert(str);
var regExp = /\s/g;
if (regExp.test(str) == true)
{
alert("a white space character has been entered");
}
}
function window_onload()
{
document.form1.tekst1.focus();
}
//-->
</script>
</head>
<body language=Javascript onLoad="window_onload()">
<form name=form1>
<input type=text name=tekst1 value=""
onkeypress="checkKey()">
<input type=button name=cmd1 value="button">
</form>
</body>
</html>
.................................................. ......
I suppose the Enter-key never reach my checkKey().What is going on? and how do I prevent this behaviour of the Enter-key? (It is quite common for a lot of users to end input of text in a text box with Enter instead of Tab)
I appreciate any answers
|