html - 仅用于字母字符的输入掩码仅允许 1 个单词在 Angular 中使用 Primeng
问题描述
就像教程说的“ a - Alpha character (defaut: A-Z,a-z)
”没关系。
但是当我mask="a"
像下面这样申请时,只让我输入 1 个单词,但我不想限制输入的大小。所以人们可以输入任何大小的单词。我怎样才能做到这一点?
我尝试使用characterPattern="[А-Zа-z]"
或提供具有常规表达式的普通 html 模式属性,^[A-Za-z]
但没有工作。提前致谢
<p-inputMask mask="a" [placeholder]="'gerekli' | translate" [(ngModel)]="User.adi"></p-inputMask>
解决方案
您可以执行以下操作,
在您的 component.ts 文件中,
setInputFilter(inputHTML, inputValue) {
[
"input",
"keyup",
"keydown",
"mouseup",
"mousedown",
"select",
"contextmenu",
"drop"
].forEach(function(event) {
inputHTML.addEventListener(event, function() {
if (inputValue(this.value)) {
this.previousValue = this.value;
this.previousSelectionStart = this.selectionStart;
this.previousSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("previousValue")) {
this.value = this.previousValue;
}
});
});
}
setInputFilter
在AfterViewInit
生命周期钩子中调用函数。
ngAfterViewInit() {
this.setInputFilter(document.getElementById("inputText"), function (value) {
return /^[a-zA-Z]*$/.test(value);
})
}
在您的 component.html 文件中,
<input id="inputText" pInputText [(ngModel)]="val"/>
我正在使用InputTextModule
PrimeNG 而不是InputMaskModule
推荐阅读
- c++ - 通过枚举成员名称调用 c++ 类
- javascript - 在循环中使用按钮切换 Span 类
- machine-learning - 在 KNeighborsClassifier 上获得奇怪的准确度 1
- python - python到exe文件上的“没有名为pygame的模块”
- redis - redis-cli 不会将请求重定向到启用了集群模式的 redis 并启用了 in-transit
- unity3d - 使用 Unity 的新输入系统移动玩家
- css - 使网站真正响应的问题
- go - 有没有办法使用 Go 标准工具在给定目录及其子目录中获取 Go 程序的所有包和依赖项?
- java - 使用 gradle 时,在 Spring Boot 测试期间无法加载驱动程序类:“org.h2.Driver”
- three.js - 在 Three.js 中拖动以旋转对象