30

I need to have an input which will remain type="text", but will open the numeric keyboard on both Android and iOS devices.

This is because the input field will still have characters such as £ and , which will not be possible within a type="number" or type="tel".

I've discovered that you can force the numeric keyboard on iOS using pattern="\d*", but this does nothing for Android.

Here is what I have so far:

<input type="text" inputmode="numeric" pattern="\d*" value="£2,000,000" />

http://jsbin.com/nelodixeza/edit?html,output

10
  • If you only want to disable validation, you can use "novalidate" attribute. Commented Feb 19, 2016 at 14:48
  • @pcagica its nothing to do with validation, I just need the numeric keyboard to open on a type=text input. Please read the question... Commented Feb 19, 2016 at 15:45
  • So I did not understand the question sorry, you why do you wan't a type text then? You can force the numeric keyboard with pattern's as you said. Commented Feb 19, 2016 at 15:57
  • I use this pattern="[0-9]*" instead of "\d*", works on both ios and android Commented Feb 19, 2016 at 15:59
  • Because it is a field for entering money with symbols such as £ and , which cannot be entered into a type=number or type=tel input Commented Feb 19, 2016 at 16:00

3 Answers 3

6
+150

Edit (In case the comments are cleaned up) this answer was written before the question specified it was for mobile web vs mobile apps. However, this answer will help those looking for a solution when using a WebView on Android.

On Android the WebView used for displaying the page can override onCreateInputConnection. With this method you can alter the imeOptions, although you are still limited to the same numeric types. Alternatively you could work on creating your own ime type.

An example:

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
    outAttrs.inputType = outAttrs.inputType | InputType.TYPE_NUMBER_VARIATION_NORMAL;
    return inputConnection;
}

However, while I'm sure the above will solve many peoples problems, I think you need something a little more complex. My suggestion is that whatever keyboard type you use, you use java to validate and format your input, which takes a couple of steps:

Setup a javascript interface

webView.addJavascriptInterface(new ValidationInterface(), "appValidator");

Create the interface class

public class ValidationInterface {

    //This method would only tell you if a char is invalid
    @JavascriptInterface
    public boolean isValid(String text){
        int len = text.length();
        //replace everything thats NOT 0-9, $, £, comma or dot
        text = text.replaceAll("[^0-9$£,\.", "");
        //Its valid if the length didnt change (because nothing needs removing)
        return text.length() == len;
    }

    //This method would strip out invalid text as you go
    @JavascriptInterface
    public String getValid(String text){
        //replace everything thats NOT 0-9, $, £, comma or dot
        return text.replaceAll("[^0-9$£,\.", "");
    }

}

Call the validation when the text changes

function validate(value) {
    //ive not written js in a long time, you'll definitely need to tweak this
    //EITHER do this
    var valid = appValidator.isValid(value);
    if (valid) {
        //DO SOEMTHING
    } ...

    //OR try this
    var validText = appValidator.getValid(value);
    $('#theField').value(validText); //really not sure this is valid
}

//also just be aware you might need to use keyUp or somethign other than change
//if you are replacing the text as you type, using getValid method
<input id="theField" type="text" onChange="validate(this.value)" ... />
Sign up to request clarification or add additional context in comments.

3 Comments

Is there anyway of getting a live example of this? I have never used Andriod's WebView so I don't know where to start. This is needed on a website, is this meant to be for an app?
Yes, this would be for a webview in an android app (because I thought you tagged the platforms and didnt mention website - I've just realised you didnt add those tags)
I've awarded the bounty to this answer, as its the only one which works (does not fit my current needs though, i.e it needs to work on a normal website).
2

I was working on something similar

have a look at my example : https://jsfiddle.net/pj2uwmtL/6/

$("input.numbers").keypress(function(event) {
  return /\d/.test(String.fromCharCode(event.keyCode));
});

5 Comments

Just spotted some odd behaviour on the fiddle I send you (Android only). If you start typing numbers it won't allow you to type any letters but if initially you type a letter it will not work. Lets see how we could get around this.
I just found this that I think is pretty much what you are after: decorplanit.com/plugin
First of all, what version of android are you testing on?
KitKat I think, 4.4.2
Sorry, couldn't find a way to switch between numeric and symbols (currency symbols) keyboard. I thought on a different way of achieving what you wanted but not sure you'll like it. jsfiddle.net/pj2uwmtL/6
1

I use:

<input type="text" pattern="\d*" />

Nothing else is necessary.

Works very well on iPhone particularly with the new keyboard types. Works with the default and Swiftkey.

In addition this works on all Android devices.

Update:

Give this a shot:

<input type="text" step="0.01" pattern="[0-9]*">

<input type="text" min="0" max="100" pattern="[0-9]*">

Or

<input type="text" pattern="\-?\d+(\.\d{0,})?">

7 Comments

This is the pattern I'm already using (see question) and pattern="\d*" does not work on Android
Will do, but real quick. Why does it have to be "text"?
It needs to be text to allow characters such as £ and , as this is for a "money" input.
I just tested your update, none of them work on Android: jsfiddle.net/3k4v38e6
There is no "money" keyboard though. I would recommend sticking with text
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.