简体   繁体   English

正则表达式密码验证angularjs

[英]regex password validation angularjs

I am new to angular js. 我是棱角分明的新手。 I have created a login screen. 我创建了一个登录屏幕。 I need to validate my password. 我需要验证我的密码。 it should contain one special character from -'$@£!%*#?&' and at least one letter and number. 它应该包含一个特殊字符-'$@£!%*#?&'以及至少一个字母和数字。 As of now it accepts all special characters without any limitations. 截至目前,它接受所有特殊字符,没有任何限制。 I have following code 我有以下代码

if (vm.newpassword_details.password.search("^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[$@£!%*#?&]).{8,}$")) {
  var msg = "Password should contain one special character from -'$@£!%*#?&' and at least one letter and number";
  alert(msg);
}

Note that your current regex imposes 4 types of restriction: 请注意,您当前的正则表达式强加了 4种类型的限制:

  1. At least one ASCII letter ( (?=.*?[A-Za-z]) ), 至少一个ASCII字母( (?=.*?[A-Za-z]) ),
  2. At least one digit ( (?=.*?[0-9]) ), 至少一位数( (?=.*?[0-9]) ),
  3. At least one specific char from the set ( (?=.*?[$@£!%*#?&]) ) 集合中至少有一个特定的字符( (?=.*?[$@£!%*#?&])
  4. The whole string should have at least 8 chars ( .{8,} ) 整个字符串应该至少有8个字符( .{8,}

The . . in .{8,} can match any char other than line break chars. in .{8,}可以匹配除换行符之外的任何字符。

If you plan to restrict the . 如果你打算限制. and only allow users to type the chars from your sets, create a superset from them and use it with RegExp#test : 并且只允许用户从你的集合中键入字符,从中创建一个超集并与RegExp#test一起使用:

if (!/^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[$@£!%*#?&])[A-Za-z0-9$@£!%*#?&]{8,}$/.test(vm.newpassword_details.password)) {  /* Error ! */  }

See the regex demo 请参阅正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM