Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

Wierd bug pressing ENTER on web

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
snocross
Asked:
snocross
  • 8
  • 5
  • 4
  • +5
1 Solution
 
HemanthaKumarCommented:
paste this html after Submit button and before any $$Return field

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

Hide it from Notes...

~Hemanth
0
 
Sjef BosmanGroupware ConsultantCommented:
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
 
snocrossAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
snocrossAuthor Commented:
... 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
 
p_parthaCommented:
Put this on the keypress event of the form:

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

Partha
0
 
Sjef BosmanGroupware ConsultantCommented:
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
 
Sjef BosmanGroupware ConsultantCommented:
Partha, bro, welcome back, but your solution isn't entirely browser-proof...
0
 
snocrossAuthor Commented:
I tried both methods and can't get either to work... :(
0
 
qwaleteeCommented:
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
 
p_parthaCommented:
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
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
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
 
CRAKCommented:
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
 
snocrossAuthor Commented:
Ok I will try this tomorrow!!  Thanks guys.
0
 
Jean Marie GeeraertsCommented:
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
 
Sjef BosmanGroupware ConsultantCommented:
This starts to look like a petshop... ;)
0
 
Jean Marie GeeraertsCommented:
Woof ,-)
0
 
Sjef BosmanGroupware ConsultantCommented:
Mimicking the cat, eh? Sprinkled with petrol and lit? ;)
0
 
snocrossAuthor Commented:
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
 
snocrossAuthor Commented:
STOP!  Got it to work!  I used Zvo's short and simple solution!  BEAUTIFUL!  Thanks everyone!
0
 
ZvonkoSystems architectCommented:
Thanks Sno :)
0
 
snocrossAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
snocrossAuthor Commented:
Ha, that's a good one!  Wish I could do that here to get a break!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 5
  • 4
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now