2

I have a text field on my HTML5 web page as follows -

<input type="text" pattern="[0-9]*" inputmode="numeric">

Please note that I cannot change the type attribute in my markup. When the user touches the text field on a mobile device I want that a numeric keypad should show instead of an alphanumeric one.

I achieved this for iOS by using the pattern attribute. Is there a way to achieve this for android?

Thanks in advance!

2 Answers 2

4

I tested this on iOS and Android and it worked!

<input type="text" inputmode="numeric">

On both devices the numeric keypad was shown, although the type of the input field is "text".

Sign up to request clarification or add additional context in comments.

1 Comment

its worked! thanks
0

As I understand it you are building a HTML form and you want mobile browsers to display a numeric keyboard instead of the full one? The correct HTML syntax (check https://www.w3schools.com/html/html_form_input_types.asp) is:

Input Type Number The defines a numeric input field. You can also set restrictions on what numbers are accepted. The following example displays a numeric input field, where you can enter a value from 1 to 5:

<form>
  Quantity (between 1 and 5):
  <input type="number" name="quantity" min="1" max="5">
</form>

Now if it were an Android form, you would put: android:inputType="numberDecimal" for example.

You said that you couldn't / didn't want to use the "number" instead of "text" attribute, but I really think that you should use attributes that are called for. Perhaps you could modify your code to accommodate the input fields with the "number" attribute or use jQuery to replace "text" with "number" on document ready.

2 Comments

I understand that the correct way is to use the input type="number". But I have a constraint that I cannot change the input type
Have you tried using javascript / jQuery to change the attribute: $("#my_input_field").attr("type", "number");

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.