在输入字段中,我添加了directive
以防止characters
进入。因此,用户只允许输入数字。现在我可以在台式机和Firefox上进行测试了。
Android移动浏览器根本无法工作。有人向我展示解决此问题的解决方法吗?
这是我的html:
<input type="text" class="form-control" restrict="true" >
指令:
import {Directive, HostListener, ElementRef, Input} from '@angular/core';
@Directive({
selector: '[restrict]'
})
export class RestrictDirective {
@Input() restrict: boolean;
constructor(private el: ElementRef, private validationService: ValidationService) {}
@HostListener('keydown', ['$event']) onKeyDown(event) {
let e = <KeyboardEvent> event;
let regexStr = '^[0-9]*$';
if (this.restrict) {
if ([46, 8, 9, 27, 13, 110, 190].indexOf(e.keyCode) !== -1 ||
(e.keyCode == 65 && e.ctrlKey === true) ||
(e.keyCode == 67 && e.ctrlKey === true) ||
(e.keyCode == 86 && e.ctrlKey === true) ||
(e.keyCode == 88 && e.ctrlKey === true) ||
(e.keyCode >= 35 && e.keyCode <= 39)) {
return;
}
let ch = String.fromCharCode(e.keyCode);
let regEx = new RegExp(regexStr);
if(regEx.test(ch))
return;
else
e.preventDefault();
}
}
}
有人帮我吗?