Link to home
Start Free TrialLog in
Avatar of Rouchie
RouchieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Disable Enter key submit from within text field (again...)

I've seen thousands of variations to this question but the only solutions that fit seem to be IE only, so here goes...

I need some javascript that I can add to a text input field that stops the form submitting when Enter is pressed.  As I'm using ASP.NET however, there are some additional requirements:

 1. I have no need to perform validation in the form (this is handled server-side by ASP.NET)
 2. I would prefer not to have to edit the <form> tag as this is inside a template.  If the code can affect only the desired text field that would be superb
 3. The solution really just needs to be a cross-browser compatible version of this:
           onkeypress="return !(window.event && window.event.keyCode == 13); }"
Avatar of Badotz
Badotz
Flag of United States of America image

Change

<input type="submit"...

to

<input type="button"...

and add ...onclick="formSubmit()"... to the input tag.

Create a function to handle the form submission:

function formSubmit()
{
    // check for whatever; if condition not met, return false, else return true
    if (something_is_not_right)
    {
        return false;
    }
    else
    {
        form.submit();
        return true;
    }
}
}
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> I need some javascript that I can add to a text input field that stops the form submitting when Enter is pressed

Only (well, 99% of the time) a submit button submits the form when {ENTER} is pressed. This sounds to me like what the poster wants to do, not suppress every occurrence of the {ENTER} key.
Avatar of Rouchie

ASKER

Zvonko - I'm very impressed with that.  It works nicely in both IE and FF (and I presume the remaining browsers...)

Thank you very much for your help.
I'm wrong again - that's 3 out of 3 today. Guess I'll hang 'em up...
Don't give up. We need you!
aw, shucks.