Angular reactive forms pattern validator adding $ to regex and breaking validation

杀马特。学长 韩版系。学妹 提交于 2019-12-01 22:23:01

You may add a .* at the end, or even revamp the pattern a bit to convert one lookahead into a consuming pattern:

Validators.pattern('(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,}')

Or, better, when using a regex for password validation, follow the principle of contrast:

Validators.pattern('(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9]*[0-9]).{8,}')

Angular will add ^ and $ on both ends of the regex pattern, so the pattern will look like

^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9]*[0-9]).{8,}$

See the regex demo

Details

  • ^ - start of string
  • (?=[^A-Z]*[A-Z]) - at least 1 uppercase ASCII letter
  • (?=[^a-z]*[a-z]) - at least 1 lowercase ASCII letter
  • (?=[^0-9]*[0-9]) - at least 1 ASCII digit
  • .{8,} - any 8 or more chars (other than line break chars)
  • $ - end of string.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!