-
Notifications
You must be signed in to change notification settings - Fork 13.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ion-input maxlength does not work with actual device keyboards #11578
Comments
Thanks for using Ionic! We will look into this. |
@jgw96 Thanks. Any news on the fix? Is this gonna be fixed in the next release? |
Still not working and is reproducible with both
|
Hi, I know it's an ugly hack, buy hopefully it's usefull for someone: import { Directive, Input } from "@angular/core"; @directive({ |
I have written custom directive to make it work in android devices. import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core';
import { Platform } from "ionic-angular";
@Directive({
selector: '[cMaxLength]'
})
export class MaxLengthDirective {
@Input('cMaxLength') cMaxLength:any;
@Output() ngModelChange:EventEmitter<any> = new EventEmitter();
constructor(public platform: Platform) {
}
//keypress event doesn't work in ionic android. keydown event will work but the value doesn't effect until this event has finished. hence using keyup event.
@HostListener('keyup',['$event']) onKeyup(event) {
const element = event.target as HTMLInputElement;
const limit = this.cMaxLength;
if (this.platform.is('android')) {
const value = element.value.substr(0, limit);
if (value.length <= limit) {
element.value = value;
} else {
element.value = value.substr(0, limit-1);
}
this.ngModelChange.emit(element.value);
}
}
@HostListener('focus',['$event']) onFocus(event) {
const element = event.target as HTMLInputElement;
if (!this.platform.is('android')) {
element.setAttribute('maxlength', this.cMaxLength)
}
}
}```
Reference:
http://jagadeeshmanne.blogspot.in/2017/08/ionic-2-angular-maxlength-issue-in.html |
The issue is not specific to Android but is to Android keyboards. maxlength works on google keyboard. Not yet tested with AOSP keyboard. |
Yes I also had issue on Samsung edge 7 device. I agree with you. I have fixed that issue and tested in multiple devices and it is working. Will edit my comment |
@mannejkumar your solution works fine but if i copy and paste any string more then the length it still accepts that string not really sure how to handle onchange on ion-input, if you have any suggestion that would help. |
Try adding 'input' event along with the 'keyup' |
This solution with directives is to complex for such a simple problem. |
@cagdas88 I agree with you if we have only one field. What if we have more than multiple fields and many across application |
Make a global export function |
|
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out. |
Ionic 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.
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:
The text was updated successfully, but these errors were encountered: