typescript 如何添加验证模式以不允许在输入 Angular2 中只留空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42994186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:22:39  来源:igfitidea点击:

How to add Validation pattern not to allow only space in input Angular2?

angulartypescript

提问by Poonam Thote

I used formBuilder in Angular2 and want to add validation pattern for not to allow "only spaces" in input.

我在 Angular2 中使用了 formBuilder 并希望添加验证模式以不允许输入中的“仅空格”。

回答by AJT82

Use the following:

使用以下内容:

Validators.pattern(/^\S*$/)

DEMO

演示

回答by Rahul Tank

try this, it will return false while sapce key press :

试试这个,它会在按下 sapce 键时返回 false:

@Component({
  selector: 'my-app',
  template: `
    <div>
       <input class="form-control" type="number" formControlName="pinCode" placeholder="Pin Code"
             (keydown.space)="$event.preventDefault()">
    </div>
  `,
  providers: [myService]
})

visit for more Events :

更多活动请访问:

https://developer.mozilla.org/en-US/docs/Web/Events

https://developer.mozilla.org/en-US/docs/Web/Events

回答by Pardeep Jain

space Not allowed

空格 不允许

let nospacePattern = [a-zA-Z0-9]

Update

更新

As per requirement in comment section.

根据评论部分的要求。

need pattern not to allow only spaces. (space in between words are allowed).but when user enter spaces in input and try to save it then it should not allow to save

需要模式不能只允许空格。(单词之间的空格是允许的)。但是当用户在输入中输入空格并尝试保存时,它不应该允许保存

Validators.pattern(".*\S.*[a-zA-z0-9 ]");

Update 2

更新 2

Better and Cleaner way to use custom validation pattern like below -

使用如下自定义验证模式的更好更简洁的方法 -

controlName: ['', [Validators.required, this.noWhitespaceValidator]],

....
....
noWhitespaceValidator(control: FormControl) {
    const isWhitespace = (control && control.value && control.value.toString() || '').trim().length === 0;
    const isValid = !isWhitespace;
    return isValid ? null : { 'whitespace': true };
  }

回答by sangRam

hey i know i am very late in party but i have also faced same issue on which i have tired following code and it works :

嘿,我知道我参加聚会很晚了,但我也遇到了同样的问题,我厌倦了以下代码并且它有效:

as per my requirement, in form comment box should be has min 30 character and that character should be non-space.so to add validation i have used following code. I am using reactive forms. in ts file, add following code

根据我的要求,在表单评论框中应该有至少 30 个字符,并且该字符应该是非空格。所以添加验证我使用了以下代码。我正在使用反应形式。在 ts 文件中,添加以下代码

comment:[''[Validators.required,Validators.minLength(30),Validators.pattern(/^((?!\s{2,}).)*$/)]]

Hey this sentence has one space between every word//this will work

Hey this sentence has one space between every word//这会起作用

Hey this sentence has more than one space between every word//this will throw err

Hey this sentence has more than one space between every word//这会抛出错误

I hope this will help somebody in future.

我希望这会在将来对某人有所帮助。