如何使用p-chips组件强制用户只输入字母、破折号和数字?该项目不允许使用p-chips以外的其他组件,用正则表达式方法替换不起作用。
输入示例: abc-123 def (允许空格)
onAdd(event) {
//this line is to limit the total chips entered
if (this.gridForm.value.inputTest.length >= 5) {
this.gridForm.value.inputTest.splice(5);
};
//replace is not working although this line was hit on breakpoint
this.gridForm.value.inputTest.forEach(m => {
m = this.gridForm.value.inputTest.replace(/[^a-z0-9]/gi, '');
})
}在Prime-NG P-CHIPS Numbers only for Array of Numbers上发现了类似的问题,但不确定如何修改它
发布于 2020-11-26 15:42:52
与其他帖子中一样,您可以检查用户条目是否与您的正则表达式匹配。如果没有,只需删除最后一个条目。以下是实现这一点的一种方式:
checkInput(event) {
if (
event.value.replace(/[^a-z0-9\-\s]/gi, "").length < event.value.length
) {
this.values.pop(); // remove last entry from values
}
}请参阅demo
https://stackoverflow.com/questions/65016995
复制相似问题