Home : Internet : JavaScript : Disable Return

How to Disable the Return Key

A common problem with website forms occurs when visitors use the Return key incorrectly. Especially in forms with multiple text fields, many people think that the Return key will move their cursor from one field to the next. Instead, the form is submitted before it has been completed.

The simple JavaScript code below prevents the Return key from submitting the form. Add the code to the head of your web page:

<script language="javascript" 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>
Subscribe to us on YouTube