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;
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)”>
call it with [input onkeypress='noenter(event);' value='xx']
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
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
Thanks, this script helped me a lot. Awesome! Just what I was looking for
[...] 38. Disable form submit on enter keypress [...]
[...] 38. Disable form submit on enter keypress [...]
Excellent, i’ve been looking for a script like this for ages…!
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
Slobodan, good code.
I captured the event in the label
It works properly for all “Enter key” pressed into the form
Thanks
text omited in the last comment:
in the label…
form id=”form1″ name=”form1″ method=”post” onKeyPress=”return disableEnterKey(event)” action=”ins_form2.asp”
nice work, first time I googled something I needed and got what I needed straight away.
[...] 38. Disable form submit on enter keypress [...]
Thanks for taking the time to share your hard work. It was just what I needed. Well Done!
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 ??
[...] 38. Disable form submit on enter keypress [...]
Thanks for the script!
[...] 14、按回车键使form表单不能提交 [...]
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!!!
just include this on your form tag…
like this
works fine on my application.
form id=”frmCreatePO” runat=”server” onkeypress=”return event.keyCode!=13″
[...] Disable form submit on enter keypress [...]
[...] 38. Disable form submit on enter keypress [...]
[...] 38. Disable form submit on enter keypress [...]
[...] 38. Disable form submit on enter keypress [...]
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