角形输入块(空格)REGEX [英] Angular Form Input block (space) REGEX

查看:46
本文介绍了角形输入块(空格)REGEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular组件中有一个输入字段,我不想让用户输入(空格).

I have an input field in my Angular component in which i want to not allow a user to be able to type a (space).

我尝试使用

<input type="text" [(ngModel)]="inputText" pattern="[a-zA-Z]">

那不是我想要的,反正还是行不通的!

which wasn't what i wanted, and it didn't work anyways!

有人知道什么是正确的正则表达式模式,仅用来阻塞(空格)键吗?正确的使用模式的方法是什么,因为上述模式不起作用...

Does anybody know what the correct regex pattern to just block the (space) key is? And what is the correct way to use the pattern, as the above pattern didn't work...

谢谢.

推荐答案

使用RegEx仍将允许用户输入空格.但是,如果将模式验证器应用于该字段,它将将该字段标记为invald.

Using RegEx will still allow the user to type in space. But it will mark the field as invald if a pattern validator is applied to it.

如果您真的不想让用户首先输入空格,则必须通过侦听输入中的keydown事件然后对其进行处理以防止其默认行为来阻止它.在这里,尝试一下:

If you don't really want to allow the user to type in space in the first place, you'll have to prevent it by listening to the keydown event on the input and then handling it to prevent its default behaviour. Here, give this a try:

<input type="text" (keydown.space)="$event.preventDefault()">

这里也是 样本StackBlitz 供您参考.

Here's also a Sample StackBlitz for your ref.

这篇关于角形输入块(空格)REGEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆