Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old November 29th, 2005, 06:09 PM
Registered User
Join Date: Nov 2005
Location: Soderup, , Denmark.
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:

.................................................. ....
<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()

<body language=Javascript onLoad="window_onload()">
<form name=form1>
<input type=text name=tekst1 value=""
<input type=button name=cmd1 value="button">
.................................................. ......

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
Reply With Quote
  #2 (permalink)  
Old November 30th, 2005, 07:28 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post

is this any use?
Reply With Quote
  #3 (permalink)  
Old November 30th, 2005, 08:22 AM
Registered User
Join Date: Nov 2005
Location: Soderup, , Denmark.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

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):

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

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.......
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Enter Key Event (Enter Key Only) Coby Excel VBA 0 February 6th, 2008 08: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

All times are GMT -4. The time now is 05:28 PM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.