#Understanding Why Reactive Forms Accept Empty Strings as Valid Values

5 messages · Page 1 of 1 (latest)

placid dew
#

Hello everyone, while working on a Reactive form, I encountered an issue where the formControl accepts an empty string as a value and marks my form as valid. Why does this happen?

here is my SB code :- https://stackblitz.com/edit/stackblitz-starters-ypcywh?file=src%2Fmain.ts

An angular-cli project based on @angular/animations, @angular/common, @angular/compiler, @angular/core, @angular/forms, @angular/platform-browser, @angular/platform-browser-dynamic, @angular/router, core-js, rxjs, tslib and zone.js

ancient vector
#

Hello, that's how 'required' works in HTML: there is no empty string here. white spaces are part of the string.

placid dew
#

@ancient vector , How can I prevent empty spaces in all of my form controls throughout the project?

ancient vector
#

No, you need to add proper validation where you need it.

placid dew
#

I'm already using the 'required' validation. Apart from this, to prevent whitespace, is there any built-in validator available, or do I need to write a custom validator?