I would like to avoid white spaces/empty spaces in my angular 2 form? Is it possible? How can this be done?
相关问题
- Angular RxJS mergeMap types
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Ignore Typescript errors in Webpack-dev-server
- How to update placeholder text in ng2-smart-table?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- 放在input的text下文本一直出现一个/(即使还没输入任何值)是什么情况
- Cannot find module 'redux' 怎么解决?
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
You can create a custom validator to handle this.
Add noWhitespaceValidator method to your component
and in the HTML
To avoid the form submition, just use
required
attr in the input fields.<input type="text" required>
Or, after submit
When the form is submited, you can use str.trim() to remove white spaces form start and end of an string. I did a submit function to show you:
If you are using Angular Reactive Forms you can create a file with a function - a validator. This will not allow only spaces to be entered.
and then in your component typescript file use the validator like this for example.
What I did was created a validator that did the samething as angular for minLength except I added the trim()
I then in my app.component.ts overrode the minLength function provided by angular
Now everywhere angular's minLength built in validator it will use the minLength that you have created in the helper.
Maybe this article can help you http://blog.angular-university.io/introduction-to-angular-2-forms-template-driven-vs-model-driven/
In this approach, you have to use FormControl then watch for value changes and then apply your mask to the value. An example should be:
Prevent user to enter space in textbox in Angular 6