Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input type number not working on ipad

I am creating input in ionic which accepts only numbers. This works on all browsers and devices except for safari and ipad. i dont want the user to input anything other than numbers.I tried to do regex validation on input event but that does not stop the user from typing in alphabets. I tried keydown event but I dont get the innerText on keyDown event. please help

like image 603
Ramya Avatar asked Jul 14 '17 07:07

Ramya


People also ask

How do you type a number on an iPad?

To enter a number or symbol quickly on an iPad, drag down on a key and then lift your finger, or switch to the numeric keyboard on iPhone. You can also enter a symbol by tapping a symbol above the keyboard.

Does input type text allow numbers?

By default, HTML 5 input field has attribute type=”number” that is used to get input in numeric format. Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.

How do you not allow input type numbers?

If you are able/allowed to use jQuery, you can disable keypress on the type='number' . $("[type='number']"). keypress(function (evt) { evt. preventDefault(); });


4 Answers

I know this is an old question, but I happened to have the same problem, and I noticed that the what worked for me in the end hasn't been provided as an answer, so I'll add the answer for the next guy.

<input type="number" min="0" inputmode="numeric" pattern="[0-9]*" title="Non-negative integral number">

This site explains the reasoning behind the solution in case you are interested.

The gist of it is that pattern="[0-9]*" is what triggers iOS to display a numeric keypad, while min and numeric are there to make sure that other OSes and browsers still work well with type="number".

Edit: iOS 12.2 for the iPad no longer supports the pattern="[0-9]*" fix - perhaps use type=tel instead.

like image 93
Sylvan D Ash Avatar answered Oct 18 '22 07:10

Sylvan D Ash


You can try this

<ion-input type="tel" inputmode="numeric" pattern="[0-9]*"></ion-input>

This'll force the input to be on numeric mode on iOS and the regex is just to ensure it'll get only numbers. As far as i know this won't work with input="number", but this is a old solution (that still works for me). So you can try with number and, if it doesn't work, fall back to tel.

Hope this helps :D

like image 31
Gabriel Barreto Avatar answered Oct 18 '22 06:10

Gabriel Barreto


if you can please provide a sample code of your input. But you can try this also

<ion-item>
    <ion-label color="primary">Inline Label</ion-label>
    <ion-input placeholder="Text Input" type="number"></ion-input>
  </ion-item>

According to Ionic Inputs

You can use "text", "password", "email", "number", "search", "tel", or "url". As the Input Type. Im sorry if i have mistaken your question. please try and provide a code sample. Thanks :)

like image 1
Diluk Angelo Avatar answered Oct 18 '22 06:10

Diluk Angelo


This worked for me:

<!-- HTML ->
<input type="number" onchange="allowOnlyNumbers()"/>
// js
function allowOnlyNumbers(e){
    // check input using regex
    var regex = RegExp(/[0-9]+/g);
    const test_result = regex.test(e.target.value);

    if(test_result){
      e.target.defaultValue = e.target.value;
    }else{
      e.target.value = e.target.defaultValue;
    }
}
like image 1
Ash Singh Avatar answered Oct 18 '22 06:10

Ash Singh