question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validators.email should allow null/empty values

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior Controls using the email validator are invalid if no value is present.

Expected behavior The email validator should allow null/empty values.

Minimal reproduction of the problem with instructions http://plnkr.co/edit/tsQ3C5tg3ba9xYAWHdLO?p=preview

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

  • Angular version: 4.x.x

  • Browser: all

  • Language: all

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:47
  • Comments:19 (4 by maintainers)

github_iconTop GitHub Comments

50reactions
davidshepherd7commented, Oct 2, 2017

For reactive forms this validator works for me:

function emailOrEmpty(control: AbstractControl): ValidationErrors | null {
    return control.value === '' ? null : Validators.email(control);
}
13reactions
rehbein-dietercommented, Jul 19, 2017

A workaround in template driven forms (set [email] to false, when value is empty. Ensure to initialize the model value with an empy string):

<input [(ngModel)]="model.email" [email]="model.email!==''">

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Validators.email allow null value - Stack Overflow
It's simply because the null /empty value will not match against the expected shape (regexp). This is the current regular expression used ...
Read more >
[Solved]-Angular Validators.email allow null value-angular.js
It's simply because the null /empty value will not match against the expected shape (regexp). This is the current regular expression used with...
Read more >
Validation - Laravel - The PHP Framework For Web Artisans
Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to...
Read more >
JavaScript : HTML form - Checking for non empty - w3resource
The following function can be used to check whether the user has entered anything in a given field. Blank fields indicate two kinds...
Read more >
validate.js
Important! One thing that is a bit unorthodox is that most validators will consider undefined values ( null and undefined ,) valid values....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found