Skip to content

Commit 006af0b

Browse files
gkalpakalxhub
authored andcommitted
docs(forms): expand e-mail validation description (#32961)
Previously, there was no documentation of what `Validators.email()` expects as a valid e-mail address, making it difficult for people to determine whether it covers their requirements or not. Even more so that the used pattern slightly deviates from the [WHATWG version](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address). One's only option was to look at the source code and try to decipher the regexp pattern. This commit adds a high-level description of the validator and mentions its similarity to and differences from the WHATWG version. It also adds a brief explanation of the regexp's behavior and references for more information in the source code to provide more context to maintainers/users trying to understand the implementation in the future. Fixes #18985 Fixes #25186 Closes #32747 PR Close #32961
1 parent 9dc4815 commit 006af0b

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎packages/forms/src/validators.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ export const NG_VALIDATORS = new InjectionToken<Array<Validator|Function>>('NgVa
5757
export const NG_ASYNC_VALIDATORS =
5858
new InjectionToken<Array<Validator|Function>>('NgAsyncValidators');
5959

60+
/**
61+
* A regular expression that matches valid e-mail addresses.
62+
*
63+
* At a high level, this regexp matches e-mail addresses of the format `local-part@tld`, where:
64+
* - `local-part` consists of one or more of the allowed characters (alphanumeric and some
65+
* punctuation symbols).
66+
* - `local-part` cannot begin or end with a period (`.`).
67+
* - `local-part` cannot be longer than 64 characters.
68+
* - `tld` consists of one or more `labels` separated by periods (`.`). For example `localhost` or
69+
* `foo.com`.
70+
* - A `label` consists of one or more of the allowed characters (alphanumeric, dashes (`-`) and
71+
* periods (`.`)).
72+
* - A `label` cannot begin or end with a dash (`-`) or a period (`.`).
73+
* - A `label` cannot be longer than 63 characters.
74+
* - The whole address cannot be longer than 254 characters.
75+
*
76+
* ## Implementation background
77+
*
78+
* This regexp was ported over from AngularJS (see there for git history):
79+
* https://github.com/angular/angular.js/blob/c133ef836/src/ng/directive/input.js#L27
80+
* It is based on the
81+
* [WHATWG version](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address) with
82+
* some enhancements to incorporate more RFC rules (such as rules related to domain names and the
83+
* lengths of different parts of the address). The main differences from the WHATWG version are:
84+
* - Disallow `local-part` to begin or end with a period (`.`).
85+
* - Disallow `local-part` length to exceed 64 characters.
86+
* - Disallow total address length to exceed 254 characters.
87+
*
88+
* See [this commit](https://github.com/angular/angular.js/commit/f3f5cf72e) for more details.
89+
*/
6090
const EMAIL_REGEXP =
6191
/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;
6292

@@ -191,6 +221,20 @@ export class Validators {
191221
* @description
192222
* Validator that requires the control's value pass an email validation test.
193223
*
224+
* Tests the value using a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
225+
* pattern suitable for common usecases. The pattern is based on the definition of a valid email
226+
* address in the [WHATWG HTML specification](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address)
227+
* with some enhancements to incorporate more RFC rules (such as rules related to domain names and
228+
* the lengths of different parts of the address).
229+
*
230+
* The differences from the WHATWG version include:
231+
* - Disallow `local-part` (the part before the `@` symbol) to begin or end with a period (`.`).
232+
* - Disallow `local-part` to be longer than 64 characters.
233+
* - Disallow the whole address to be longer than 254 characters.
234+
*
235+
* If this pattern does not satisfy your business needs, you can use `Validators.pattern()` to
236+
* validate the value against a different pattern.
237+
*
194238
* @usageNotes
195239
*
196240
* ### Validate that the field matches a valid email pattern

0 commit comments

Comments
 (0)