使用正则表达式进行角度模式验证 [英] Angular pattern validation with regex

查看:169
本文介绍了使用正则表达式进行角度模式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个密码模式验证器

I need to make a password pattern validator

密码应具有:

  • 1个大写字母
  • 1个小写字母
  • 一个数字
  • 最小长度为8.

我发现了这种正则表达式模式:

I found this regex pattern:

Validators.pattern('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d!$%@#£€*?&]')

但是,验证器总是声称我的输入无效

however, the validator always claims my input is invalid

errors:    
   pattern:
      actualValue: "Test1234"
      requiredPattern: "^/^(?=.*[A-Za-z])(?=.*d)[A-Za-zd!$%@#£€*?&]$"

根据 https://regex101.com/r/AfAdKp/1 ,此值为应该是有效的.

according to https://regex101.com/r/AfAdKp/1 this value is supposed to be valid.

编辑:为了澄清,Test1234应该可以正常工作

to clarify, Test1234 is supposed to work

推荐答案

您当前的正则表达式存在多个问题:

You have multiple issues with your current regex:

  1. 您的正则表达式没有字符类的量词
  2. Validators.pattern不需要定界符,但您一开始就扔了一个定界符
  3. 您应该将转义令牌加倍.
  1. Your regex doesn't have a quantifier for character class
  2. Validators.pattern doesn't need delimiters but you threw one at beginning
  3. You should double escape tokens.

您需要的是这个

Validators.pattern('^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=\\D*\\d)[A-Za-z\\d!$%@#£€*?&]{8,}$');

在此处查看实时演示

这篇关于使用正则表达式进行角度模式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆