Tagged with android

Android and Number Inputs in Forms

I had a form with a single input box and a hidden submit button that I had been using for getting user input in a form on a WebView. The input was supposed to be a number so I wanted the number soft keyboard to come up when the field was selected. Simply changing the input type to number gave me the functionality I wanted. Unfortunately, once I did this, pressing enter no longer submitted the form. ┬áIt appears the numeric keyboard on Android doesn’t do a submit (at least, if the input type is number).

Google searches were pretty fruitless, though I did find this unanswered StackOverflow question.

I did confirm that the keyboard doesn’t send any keys whatsoever. I was at a bit of a loss as to how to fix this problem.

The solution I hit on, which may be sub-optimal but met the needs I had, was to add a text input field and position it off the screen. I then added an onfocus script that set the text input to disabled and submitted the form. This gave me the behavior I wanted and didn’t add extraneous fields to the form. One more Android bug tackled.

Here’s the code and a sample page:

function numberFix() {
    numfix.disabled = true;
    theform.submit();
}
<form id="theform"><input id="thetext" type="number" />
  <input id="numfix" style="margin-left: -999px;" onfocus="numberFix()" type="text" />
  <input style="height: 0px; width: 0px; border: none; padding: 0px;" tabindex="999" type="submit" />
</form>
Tagged , ,