Skip to main content
edited body
Source Link
Wiktor Stribiżew
  • 630.7k
  • 41
  • 501
  • 629

Use the following fix:

Validators.pattern(/^\+?(?:[1-9]\d*(?:\.\d{1,2})?|0\.(?:[1-9]\d?|\d[1-9]))$/)

The regex demo is here.

Make sure:

  • You define the regex as a regex literal (not a string, /.../ should not be wrapped with any quotes
  • If you use a string pattern, make sure you double escape the backslashes and then, you do not need to use ^ and $ at both ends as they will be added automatically.

The vodecode above is equal to

Validators.pattern("\\+?(?:[1-9]\\d*(?:\\.\\d{1,2})?|0\\.(?:[1-9]\\d?|\\d[1-9]))")

Use the following fix:

Validators.pattern(/^\+?(?:[1-9]\d*(?:\.\d{1,2})?|0\.(?:[1-9]\d?|\d[1-9]))$/)

The regex demo is here.

Make sure:

  • You define the regex as a regex literal (not a string, /.../ should not be wrapped with any quotes
  • If you use a string pattern, make sure you double escape the backslashes and then, you do not need to use ^ and $ at both ends as they will be added automatically.

The vode above is equal to

Validators.pattern("\\+?(?:[1-9]\\d*(?:\\.\\d{1,2})?|0\\.(?:[1-9]\\d?|\\d[1-9]))")

Use the following fix:

Validators.pattern(/^\+?(?:[1-9]\d*(?:\.\d{1,2})?|0\.(?:[1-9]\d?|\d[1-9]))$/)

The regex demo is here.

Make sure:

  • You define the regex as a regex literal (not a string, /.../ should not be wrapped with any quotes
  • If you use a string pattern, make sure you double escape the backslashes and then, you do not need to use ^ and $ at both ends as they will be added automatically.

The code above is equal to

Validators.pattern("\\+?(?:[1-9]\\d*(?:\\.\\d{1,2})?|0\\.(?:[1-9]\\d?|\\d[1-9]))")
Source Link
Wiktor Stribiżew
  • 630.7k
  • 41
  • 501
  • 629

Use the following fix:

Validators.pattern(/^\+?(?:[1-9]\d*(?:\.\d{1,2})?|0\.(?:[1-9]\d?|\d[1-9]))$/)

The regex demo is here.

Make sure:

  • You define the regex as a regex literal (not a string, /.../ should not be wrapped with any quotes
  • If you use a string pattern, make sure you double escape the backslashes and then, you do not need to use ^ and $ at both ends as they will be added automatically.

The vode above is equal to

Validators.pattern("\\+?(?:[1-9]\\d*(?:\\.\\d{1,2})?|0\\.(?:[1-9]\\d?|\\d[1-9]))")