Web Connection
Annoying link after hitting enter button
Gravatar is a globally recognized avatar based on your email address. Annoying link after hitting enter button
  BrianWg
  All
  Feb 22, 2017 @ 02:09pm

I have a problem with a page that has a logout button and textboxes - if someone types text into the textbox and then hits the enter key - it activates the logout button ?????

So I created a test page with one text box and a button and this seems to do exactly the same thing

Is the page

is the code behind

when I type ant text in the box and hit enter the logout button is triggered ???

How cam I stop this???

Gravatar is a globally recognized avatar based on your email address. re: Annoying link after hitting enter button
  Rick Strahl
  BrianWg
  Feb 22, 2017 @ 04:42pm

That's standard behavior for Web pages. When you press Enter the first Submit button will be accessed and the active form is submitted. The same is true on this form here for example. Textareas (ie. multi-line textboxes) can accept Enter though.

You can work around this with some JavaScript by effectively disabling the form submit unless you explictly click the button.

Something like this:

<form id="form1" onsubmit="formSubmit(false)" />


     <input type="submit" name="btnSubmit" id="btnSubmit" onclick="formSubmit(true)" />
     
</form>
<script>
function formSubmit(force) { 
    if (!force)
        return;
      
    $("#form1").submit();
}    
</script>

But I would really advise against that. Web Forms are expected to submit on Enter operations of textboxes, so when you change behavior you essentially break the Web...

+++ Rick ---

© 1996-2024