Solved

Wierd bug pressing ENTER on web

Posted on 2004-04-29
24
424 Views
Last Modified: 2013-12-18
I have an order form on the web with ONE editable field for quantity.  The form has one button called ORDER ITEM that validates the quantity (via Javascript) and submits the form.  The problem is users are pressing the ENTER key rather than pressing the button which is bypassing my validation!

I was baffled because I have many other web apps and never seen this problem before.  Now I have discovered if I add an extra editable field it is not a problem but the extra field looks ugly and doesn't belong.  It seems that if there is more than one field then ENTER does nothing however if I have only ONE field then ENTER acts like a SUBMIT button!

Anybody have this problem happen to them, and is there a workaround besides adding another blank editable field?
0
Comment
Question by:snocross
  • 8
  • 5
  • 4
  • +5
24 Comments
 
LVL 24

Expert Comment

by:HemanthaKumar
Comment Utility
paste this html after Submit button and before any $$Return field

[<form method = "get"><input type = "hidden"></form>]

Hide it from Notes...

~Hemanth
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
There is another workaround: catch the Enter-key in JavaScript. I forgot how to do this, but I'll be back (if someone else isn't here before me...)
0
 
LVL 5

Author Comment

by:snocross
Comment Utility
Heman that worked but unfortunately it worked too well!!  Enter does not submit the form however clicking the actual ORDER ITEM button now just pushes it down and it gets stuck down without doing anything!
0
 
LVL 5

Author Comment

by:snocross
Comment Utility
... maybe I should tell you my ORDER ITEM button is not a notes button but rather a field with the following code;

"Type = hidden><input type=\"Button\" value = \"Order Item\" onClick=\"saveSubmit()\""
0
 
LVL 14

Expert Comment

by:p_partha
Comment Utility
Put this on the keypress event of the form:

if(window.event.keyCode==13)
{
window.event.returnValue = false
}

Partha
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
Put this in the onKeyPress event of the form:
    return noEnter();

and in the JS Header:
    function noEnter() {
        if(event.keyCode) {
            if(event.keyCode=="13")
                return false;
        } else if(event.which) {
            if(event.which=="13")
                return false;
        }
    }
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
Partha, bro, welcome back, but your solution isn't entirely browser-proof...
0
 
LVL 5

Author Comment

by:snocross
Comment Utility
I tried both methods and can't get either to work... :(
0
 
LVL 31

Expert Comment

by:qwaletee
Comment Utility
Restructure slightly.

Take the saveSubmit code, and remove the submit() call.  Instead, have it return false if validation fails, and return true if validation succeeds.

Set the form onsubmit even to point to saveSubmit

Change the Order Item button.  instead of calling saveSubmit, it should just issue a submit call.  Now, when the user clicks the button, all it does is submit, which is the same thing the enter key does.  Your validation runs because it is assigned to the onsubmit event, and the return false prevents the final submit from occuring if it should not.

Everything else is a kludge, including the other suggestions here (trapping the enter key or putting in extra design elements that cause the browser to not allow enter to be used as submit).
0
 
LVL 14

Expert Comment

by:p_partha
Comment Utility
SNO,
Where did u put that code, You have to put it in form "Onkey press event", Also as qwal pointed out, u can even call the validate function , i.e


       if(event.keyCode) {
           if(event.keyCode=="13")
"  <call your function here>"

       } else if(event.which) {
           if(event.which=="13")
"  <call your function here>"
       }

SJef, yeah long time.. will ping u sometime today ;-)

partha
 
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Hello Sno,

here my proposal.

Put this into JSHeader section:
function checkKey(e){
  keyCode = (e.which)? e.which: e.keyCode;
  if(keyCode==13) return false;
}


Puthis in your Field onKeyPress event:
return checkKey(event)


That should do.
Tested in IE6.0 and Mozilla1.5


0
 
LVL 63

Accepted Solution

by:
Zvonko earned 50 total points
Comment Utility
Ok, I just saw partha's comment.
It can be all done in Field's onKeyPress event.
This is all you need to put into Field's onKeyPress event handler:

keyCode = (event.which)? event.which: event.keyCode;
return (keyCode!=13)

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 13

Expert Comment

by:CRAK
Comment Utility
Hey... I saw this one last thursday. I just solved the thing two weeks earlier at work and thought "Bummer.... an easy score and I can't reach the code for another 3 days"....
So the quest is still on?!
;-))

I trimmed my code a bit. The remainder must look familiar to you (i.e. all participants here):

JS Header:

function capEnter(fld,evnt)
{
      key = document.layers ?evnt.which :document.all ?evnt.keyCode :envt.keyCode;
      if (key != 13)
      {
            // no enter -> accept input / continue processing pressed key
            return(true);
      }
      else
      {
            // enter -> do not continue processing pressed key
            return(false);
      }
}


Field:
<INPUT Type=\"Text\" Name=\"NavJump\" onKeyDown=\"return(capEnter(this,event))\">

It's the onKeyDown event that you need....


(Somehow I feel like a parrot!)
0
 
LVL 5

Author Comment

by:snocross
Comment Utility
Ok I will try this tomorrow!!  Thanks guys.
0
 
LVL 8

Expert Comment

by:Jean Marie Geeraerts
Comment Utility
Guess I got to late to get the points for this one, but CRAK is right, you have to check for the enter key in onkeydown or onkeyup event rather than onkeypress to get it to work.
You can actually use the same event on form level to force a submit from anywhere on the form when pressing enter.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
This starts to look like a petshop... ;)
0
 
LVL 8

Expert Comment

by:Jean Marie Geeraerts
Comment Utility
Woof ,-)
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
Mimicking the cat, eh? Sprinkled with petrol and lit? ;)
0
 
LVL 5

Author Comment

by:snocross
Comment Utility
Crak, I just tried your most recent example here but not clear on the FIELD part.  I tried pasting that into an editable field and there were problems with the quotes.  I played with the quotes and just got a blank editable field on my form.  Of course everything works at this point because then I have TWO editable fields.  My problem only occurs when there is only one editable field on the form.  Can you be extra specific on where I should put the following text;

<INPUT Type=\"Text\" Name=\"NavJump\" onKeyDown=\"return(capEnter(this,event))\">

0
 
LVL 5

Author Comment

by:snocross
Comment Utility
STOP!  Got it to work!  I used Zvo's short and simple solution!  BEAUTIFUL!  Thanks everyone!
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Thanks Sno :)
0
 
LVL 5

Author Comment

by:snocross
Comment Utility
No, thank you!  What a headache that was!  Users were bypassing my validation and bringing inventory levels below ZERO!  Granted we had an approval process where we caught the problems but this will make it so easy to prevent.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Such stories make me laughing.
Normally you would assume those form users are no gambler.
But from my real life I heard from one bank account girl that they know all in the office which button on the console to press, all girls in the office at the same time, to get a break because the computer went down :)

0
 
LVL 5

Author Comment

by:snocross
Comment Utility
Ha, that's a good one!  Wish I could do that here to get a break!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You’ve got a lotus Domino web server, and you have been told that “leverage browser caching” is a must do. This means that we have to tell the browser everywhere in the web to use cache. In other words, we set (and send) an expiration date in the HT…
IBM Notes offer Encryption feature using which the user can secure its NSF emails or entire database easily. In this section we will discuss about the process to Encrypt Incoming and Outgoing Mails in depth.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now