I need to use JS function to disable enter key. I'm currently facing an issue with my Spring form
<form:textarea path="name" onkeypress="return noenter()"
here is the function I currently using
<script> function noenter() { alert("Testing");
return !(window.event && window.event.keyCode == 13);
} </script>
for some reason, the alert is working when I press on Enter key, but still facing same exception
HTTP Status 415 -
type Status report
message
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (). Apache Tomcat/6.0.29
I need to use JS function to disable enter key. I'm currently facing an issue with my Spring form
<form:textarea path="name" onkeypress="return noenter()"
here is the function I currently using
<script> function noenter() { alert("Testing");
return !(window.event && window.event.keyCode == 13);
} </script>
for some reason, the alert is working when I press on Enter key, but still facing same exception
Share Improve this question edited May 16, 2011 at 12:45 Sean Patrick Floyd 299k71 gold badges475 silver badges595 bronze badges asked May 16, 2011 at 12:26 Ali Taha Ali MahboubAli Taha Ali Mahboub 3,5116 gold badges28 silver badges25 bronze badges 2HTTP Status 415 -
type Status report
message
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (). Apache Tomcat/6.0.29
-
window.event
is not cross-browser I think. It's an IE thing... – Šime Vidas Commented May 16, 2011 at 12:29 - Is there something peculiar about the textareas you are using? Typically, an HTML textarea element turns ENTER into newlines, rather than submitting the form, no javascript needed. – Doug Kavendek Commented May 16, 2011 at 17:56
3 Answers
Reset to default 10This should work:
Markup:
<form:textarea path="name" onkeypress="return noenter(event)">
JavaScript:
function noenter(e) {
e = e || window.event;
var key = e.keyCode || e.charCode;
return key !== 13;
}
Live demo: http://jsfiddle/Fj3Mh/
you need to address this in the keyup and keydown event. The browser uses the enter key independent of the actual web page. So you will need to stop event propagation at the keyup or keydown event. By the time the keypress event has been emitted the browser itself has received it and there is no way to keep it from processing. This is specific to the enter key and a few others. Character keys such as 'a' and 'b' do not suffer from this problem.
<script type="text/javascript">
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;
</script>