Sunday, June 5, 2011

How to disable the Enter key while doing form posting?



I got a problem last week in IE browser only which blocked my production roll out. As form fields, i had a quantity field with one form refresh to update the quantity and another form button to submit.

Order of my fields- Quantity , Refresh button and submit button.
We didn't specify any tab order. In all the browsers after entering the quantity,if user click enter button, it went to update the quantity. And only in this crapy IE, it went for form submit.

Here is my simple javascript which helps to solve this problem


function handleEnter(inField, e) {
var charCode;

if(e && e.which){
charCode = e.which;
}else if(window.event){
e = window.event;
charCode = e.keyCode;
}

if(charCode == 13) {
alert("We will update the quantity here what happen today: " + inField.id);
}

}

Attaching the event to field
Quantity: input type="text" id="someInput" onkeypress="handleEnter(this, event)"


Did they not code properly, while writing their user agents or did they handle unwanted usecase in which my case fell down? Why this Crap alone behaving like this?