ion-input maxlength does not work with actual device keyboards
See original GitHub issueIonic version: (check one with “x”) [ ] 1.x [ ] 2.x [ x] 3.x
I’m submitting a … (check one with “x”) [ x] bug report [ ] feature request [ ] support request
Current behavior: The maxlength attribute is not working on ion-input with types other than password. This is only happening on actual devices. Browser and emulators are working just fine.
Expected behavior: The user should not be able to type in any more characters when they reach the maxlength limit.
Steps to reproduce: Cannot reproduce on a browser.
Related code: A sample of the code.
<ion-input type="text" [(ngModel)]="newHomePhone" autocomplete="off" autocorrect="off"
autocapitalize="off" spellcheck="false" name="newHomePhone"
maxlength="10">
</ion-input>
Other information: I have tried different input types (Email, text, …) but only password works. This is affecting both Android and iOS platforms. I was getting the same issue even before cordova 7.0.0 update. So it should not be related to the new cordova. An interesting observation was that when I was debugging the app and used my desktop keyboard (using chrome remote device for Android), the maxlength limit was being applied. But at the same time when I used the device keyboard, it was not working!
Ionic info:
Your system information:
Cordova CLI: 7.0.0
Ionic Framework Version: 3.1.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.3.7
ios-deploy version: 1.9.1
ios-sim version: 5.0.13
OS: macOS Sierra
Node Version: v7.2.0
Xcode version: Xcode 8.3.2 Build version 8E2002
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:14 (1 by maintainers)
Top Related StackOverflow Question
Hi, I know it’s an ugly hack, buy hopefully it’s usefull for someone:
import { Directive, Input } from “@angular/core”;
@Directive({ selector: ‘[limit-to]’, host: { ‘(blur)’: ‘_onBlur($event)’, ‘(keyup)’: ‘_onKeyup($event)’, ‘(focus)’: ‘_onFocus($event)’, } }) export class LimitToDirective { @Input(‘limit-to’) limitTo; _onBlur(e) { const limit = +this.limitTo; if (e.target.value.length > limit) { e.target.value = e.target.value.substring(0, 15); } }; _onFocus(e) { const limit = +this.limitTo; if (e.target.value.length > limit) { e.target.value = e.target.value.substring(0, 15); } }; _onKeyup(e) { const limit = +this.limitTo; if (e.target.value.length > limit) { e.target.value = e.target.value.substring(0, 15); } }; }
I have written custom directive to make it work in android devices.