Disable form submit on enter keypress

Posted By Slobodan Kovacevic on March 13, 2005

I needed a JavaScript function that disables form submission when enter key is pressed. I’ve looked around and all solutions worked only in IE and I needed it to work in FireFox as well.

Finally after some more reading I found a solution how it can be done. Here is the function that I created:

<script language="JavaScript">
function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}
</script>

All you need to do is to add onKeyPress event to, for example, text element and call this function. Something like this:

<input type=”text” name=”mytext” onKeyPress=”return disableEnterKey(event)”>

About the author

Slobodan Kovacevic

Comments

75 Responses to “Disable form submit on enter keypress”

  1. GDmac says:

    call it with [input onkeypress='noenter(event);' value='xx']

  2. Gurvinder Pal Singh says:

    Hi , I need to implement + key for inserting a line in the textbox and key for submitting the form…if any one could help me

  3. Gurvinder Pal Singh says:

    Hi , I need to implement ctrl+entr key for inserting a line in the textbox and entr key for submitting the form…if any one could help me

  4. greatcan says:

    Thanks, this script helped me a lot. Awesome! Just what I was looking for :D

  5. [...] 38. Disable form submit on enter keypress [...]

  6. [...] 38. Disable form submit on enter keypress [...]

  7. Excellent, i’ve been looking for a script like this for ages…!

  8. Anonymous says:

    this was really handy. I could add the onKeyPress event in order to nullify the return key, and another onKeyUp to work my autocomplete file

  9. Xavier Vallverdú. Tarragona. Spain says:

    Slobodan, good code.

    I captured the event in the label

    It works properly for all “Enter key” pressed into the form

    Thanks

  10. Xavier Vallverdú. Tarragona. Spain says:

    text omited in the last comment:

    in the label…

    form id=”form1″ name=”form1″ method=”post” onKeyPress=”return disableEnterKey(event)” action=”ins_form2.asp”

  11. Kier says:

    nice work, first time I googled something I needed and got what I needed straight away.

  12. Jeff Sailers says:

    Thanks for taking the time to share your hard work. It was just what I needed. Well Done!

  13. Cor Knops says:

    Hello Slobodan,

    I tried your script but it doesn’t work… i.e. the form is sent when I hit the enter-key. I have built it in in a ASP-file which contains both VB- as well as Java-script. Does this influence the functioning of your script ??

  14. [...] 38. Disable form submit on enter keypress [...]

  15. Thanks for the script!

  16. [...] 14、按回车键使form表单不能提交 [...]

  17. Congratulations! I was search by a lot of websites, but this is the unic that explanation about the way to call this function: onKeyPress=”return disableEnterKey(event)”

    The RETURN expression on the calling fix my problems with mozila.

    Thank you!!!

  18. Jepoy says:

    just include this on your form tag…

    like this

    works fine on my application.

  19. Jepoy says:

    form id=”frmCreatePO” runat=”server” onkeypress=”return event.keyCode!=13″

  20. [...] Disable form submit on enter keypress [...]

  21. [...] 38. Disable form submit on enter keypress [...]

  22. Anurag says:

    page does not refresh when submitting the form in ruby on rails here is the code for view and controller
    view -

    logins_path do |f| -%>

    Email

    Password

    Confirm Password

    ‘login’, :label => “(human authentication)”) %>

    controller -

    class LoginsController [:index]

    # render new.rhtml

    def new
    end

    def create
    cookies.delete :auth_token
    # protects against session fixation attacks, wreaks havoc with
    # request forgery protection.
    # uncomment at your own risk
    # reset_session
    p”mmmmmm”
    @login = Login.new(params[:login])
    @login.captcha= params[:login][:captcha]
    @login.captcha_key = params[:login][:captcha_key]
    session[:login_id]= @login.id
    if @login.save_with_captcha
    self.current_login = @login
    Notifier.deliver_confirmation_email(@login,confirmation_hash(@login.email))
    @login.confirm_hash=confirmation_hash(@login.email)
    @login.save
    #redirect_back_or_default(’/')
    #flash[:notice] = “Thanks for signing up!”
    render :text => ‘Thanks for Registrations! check your email for confirmation’
    else
    #rescue ActiveRecord::RecordInvalid
    render :action => ‘new’
    end
    end

    def confirmation_hash(string)
    Digest::SHA1.hexdigest(string + “secret word”)
    end

    def confirmation_email
    @login = Login.find(params[:login_id])
    @login.save
    # if the passed hash matches up with a User, confirm him, log him in, set proper flash[:notice], and stop looking
    if @login.confirm_hash== (params[:hash]).to_s
    # @login.update_attributes(:email_confirmed => true)
    @login.email_confirmed=1
    @login.save
    flash[:notice] = “Email #{@login.email} is confirmed Welcome #{@login.email}”

    else
    flash[:notice] = “Not confirmed.”
    end
    end
    def password_requested

    end
    def new_password
    length = 6
    chars = (’a’..’z').to_a + (’A’..’Z').to_a + (’1′..’9′).to_a - ['o', 'O', 'i', 'I']

    Array.new(length) { chars[rand(chars.size)] }.join
    @newpass = “”
    1.upto(length) { |i| @newpass ‘login’, :action => ‘index’
    #end
    else
    flash[:notice] = (’email address not found’)
    end
    # set password

    end

    def confirm_password
    @login=Login.find(params[:login_id])
    @login.crypted_password=@login.encrypt(params[:newpass])
    if @login.save
    flash[:notice] = “password is confirmed”
    else
    flash[:notice] = “Not change.”
    end
    end
    end

\n\n -->