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
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.
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.
If you are able/allowed to use jQuery, you can disable keypress on the type='number' . $("[type='number']"). keypress(function (evt) { evt. preventDefault(); });
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.
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
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 :)
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With