Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 29th, 2005, 07:09 PM
Registered User
 
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old November 30th, 2005, 08:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

is this any use?
http://p2p.wrox.com/topic.asp?TOPIC_ID=3672
 
Old November 30th, 2005, 09:22 AM
Registered User
 
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes - I could definitely use that hint.(But I can see that I need to find/know something about the Event-object??). Thank you. Meanwhile I have been on the WEB and found this solution (I suppose it's essential the same thing going on as in your solution):

<body>
<form name=form1>
<input type=text name=tekst1 value=""
 onkeypress="{if (event.keyCode==13)event.returnValue=false;}">
</form>

This will work for the text box - and in the textarea box you just omit the onkeypress thus making the Enter-key a valid key.......
SteenH





Similar Threads
Thread Thread Starter Forum Replies Last Post
Enter Key Event (Enter Key Only) Coby Excel VBA 0 February 6th, 2008 09:55 PM
enter key within text area shs BOOK: Beginning ASP.NET 1.0 0 April 12th, 2004 11:08 PM
Enter Key rwalker VB How-To 5 April 7th, 2004 11:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.